public final class TG_Fiber extends Object implements Runnable
JAX-WS RI is capable of running a large number of request/response concurrently by
using a relatively small number of threads. This is made possible by utilizing
a TG_Fiber — a user-level thread that gets created for each request/response
processing.
A fiber remembers where in the pipeline the processing is at, what needs to be executed on the way out (when processing response), and other additional information specific to the execution of a particular request/response.
Fiber can be suspended by a Tube.
When a fiber is suspended, it will be kept on the side until it is
resumed. This allows threads to go execute
other runnable fibers, allowing efficient utilization of smaller number of
threads.
FiberContextSwitchInterceptor allows Tubes and Adapters
to perform additional processing every time a thread starts running a fiber
and stops running it.
Just like thread, a fiber has a context class loader (CCL.) A fiber's CCL becomes the thread's CCL when it's executing the fiber. The original CCL of the thread will be restored when the thread leaves the fiber execution.
Because TG_Fiber doesn't keep much in the call stack, and instead use
conts to store the continuation, debugging fiber related activities
could be harder.
Setting the LOGGER for FINE would give you basic start/stop/resume/suspend
level logging. Using FINER would cause more detailed logging, which includes
what tubes are executed in what order and how they behaved.
When you debug the server side, consider setting serializeExecution
to true, so that execution of fibers are serialized. Debugging a server
with more than one running threads is very tricky, and this switch will
prevent that. This can be also enabled by setting the system property on.
See the source code.
| Modifier and Type | Class and Description |
|---|---|
static interface |
TG_Fiber.CompletionCallback
Callback to be invoked when a
TG_Fiber finishs execution. |
| Modifier and Type | Field and Description |
|---|---|
TG_Engine |
owner |
static boolean |
serializeExecution
Set this boolean to true to execute fibers sequentially one by one.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addInterceptor(TG_FiberContextSwitchInterceptor interceptor)
Adds a new
FiberContextSwitchInterceptor to this fiber. |
static TG_Fiber |
current()
Gets the current fiber that's running.
|
ClassLoader |
getContextClassLoader()
Gets the context
ClassLoader of this fiber. |
com.sun.xml.ws.api.message.Packet |
getPacket()
Gets the current
Packet associated with this fiber. |
boolean |
isAlive()
Returns true if this fiber is still running or suspended.
|
static boolean |
isSynchronous()
(ADVANCED) Returns true if the current fiber is being executed synchronously.
|
boolean |
removeInterceptor(com.sun.xml.ws.api.pipe.FiberContextSwitchInterceptor interceptor)
Removes a
FiberContextSwitchInterceptor from this fiber. |
void |
resume(com.sun.xml.ws.api.message.Packet resumePacket)
Wakes up a suspended fiber.
|
void |
resume(Throwable throwable)
Wakes up a suspended fiber with an exception.
|
void |
run()
Deprecated.
|
com.sun.xml.ws.api.message.Packet |
runSync(com.sun.xml.ws.api.pipe.Tube tubeline,
com.sun.xml.ws.api.message.Packet request)
Runs a given
Tube (and everything thereafter) synchronously. |
ClassLoader |
setContextClassLoader(ClassLoader contextClassLoader)
Sets the context
ClassLoader of this fiber. |
void |
start(com.sun.xml.ws.api.pipe.Tube tubeline,
com.sun.xml.ws.api.message.Packet request,
TG_Fiber.CompletionCallback completionCallback)
Starts the execution of this fiber asynchronously.
|
String |
toString() |
public final TG_Engine owner
public static volatile boolean serializeExecution
public void start(@NotNull
com.sun.xml.ws.api.pipe.Tube tubeline,
@NotNull
com.sun.xml.ws.api.message.Packet request,
@Nullable
TG_Fiber.CompletionCallback completionCallback)
This method works like Thread.start().
tubeline - The first tube of the tubeline that will act on the packet.request - The request packet to be passed to startPoint.processRequest().completionCallback - The callback to be invoked when the processing is finished and the
final response packet is available.runSync(Tube,Packet)public void resume(@NotNull
com.sun.xml.ws.api.message.Packet resumePacket)
If a fiber was suspended without specifying the next Tube,
then the execution will be resumed in the response processing direction,
by calling the Tube.processResponse(Packet) method on the next/first
Tube in the TG_Fiber's processing stack with the specified resume
packet as the parameter.
If a fiber was suspended with specifying the next Tube,
then the execution will be resumed in the request processing direction,
by calling the next tube's Tube.processRequest(Packet) method with the
specified resume packet as the parameter.
This method is implemented in a race-free way. Another thread can invoke
this method even before this fiber goes into the suspension mode. So the caller
need not worry about synchronizing NextAction.suspend() and this method.
resumePacket - packet used in the resumed processingpublic void resume(@NotNull
Throwable throwable)
The execution of the suspended fiber will be resumed in the response
processing direction, by calling the Tube.processException(Throwable) method
on the next/first Tube in the TG_Fiber's processing stack with
the specified exception as the parameter.
This method is implemented in a race-free way. Another thread can invoke
this method even before this fiber goes into the suspension mode. So the caller
need not worry about synchronizing NextAction.suspend() and this method.
throwable - exception that is used in the resumed processingpublic void addInterceptor(@NotNull
TG_FiberContextSwitchInterceptor interceptor)
FiberContextSwitchInterceptor to this fiber.
The newly installed fiber will take effect immediately after the current
tube returns from its Tube.processRequest(Packet) or
Tube.processResponse(Packet), before the next tube begins processing.
So when the tubeline consists of X and Y, and when X installs an interceptor, the order of execution will be as follows:
public boolean removeInterceptor(@NotNull
com.sun.xml.ws.api.pipe.FiberContextSwitchInterceptor interceptor)
FiberContextSwitchInterceptor from this fiber.
The removal of the interceptor takes effect immediately after the current
tube returns from its Tube.processRequest(Packet) or
Tube.processResponse(Packet), before the next tube begins processing.
So when the tubeline consists of X and Y, and when Y uninstalls an interceptor on the way out, then the order of execution will be as follows:
@Nullable public ClassLoader getContextClassLoader()
ClassLoader of this fiber.public ClassLoader setContextClassLoader(@Nullable ClassLoader contextClassLoader)
ClassLoader of this fiber.@Deprecated public void run()
TG_Fiber.@NotNull
public com.sun.xml.ws.api.message.Packet runSync(@NotNull
com.sun.xml.ws.api.pipe.Tube tubeline,
@NotNull
com.sun.xml.ws.api.message.Packet request)
Tube (and everything thereafter) synchronously.
This method blocks and returns only when all the successive Tubes
complete their request/response processing. This method can be used
if a Tube needs to fallback to synchronous processing.
class FooTube extends AbstractFilterTubeImpl {
NextAction processRequest(Packet request) {
// run everything synchronously and return with the response packet
return doReturnWith(Fiber.current().runSync(next,request));
}
NextAction processResponse(Packet response) {
// never be invoked
}
}
tubeline - The first tube of the tubeline that will act on the packet.request - The request packet to be passed to startPoint.processRequest().start(Tube, Packet, CompletionCallback)@Nullable public com.sun.xml.ws.api.message.Packet getPacket()
Packet associated with this fiber.
This method returns null if no packet has been associated with the fiber yet.
public boolean isAlive()
public static boolean isSynchronous()
Fiber may run synchronously for various reasons. Perhaps this is on client side and application has invoked a synchronous method call. Perhaps this is on server side and we have deployed on a synchronous transport (like servlet.)
When a fiber is run synchronously (IOW by runSync(Tube, Packet)),
further invocations to runSync(Tube, Packet) can be done
without degrading the performance.
So this value can be used as a further optimization hint for
advanced Tubes to choose the best strategy to invoke
the next Tube. For example, a tube may want to install
a FiberContextSwitchInterceptor if running async, yet
it might find it faster to do runSync(Tube, Packet)
if it's already running synchronously.
@NotNull public static TG_Fiber current()
This works like Thread.currentThread().
This method only works when invoked from Tube.
Copyright © 2006–2015 TinyGroup. All rights reserved.