users@grizzly.java.net

Re: attaching user data to GrizzlyWorkerThread

From: John ROM <snake-john_at_gmx.de>
Date: Thu, 14 Aug 2008 13:25:35 +0200

>
>
> its possible to attach user data to GrizzlyWorkerThread and make them
> persistent between different requests served by same worker? Something like
> Context.AttributeScope.WORKER

WorkerThread is designed to not hold on to State inbetween different Requests.


>
> is WorkerThread.getAttachment().getAttribute() what i need?
>
This would only store data for the current WorkerThread transaction.

You can store user data with the help of Grizzly Context :
AttributeHolder connectionAttrs =
                ctx.getAttributeHolderByScope(...);

Where
Context.AttributeScope.REQUEST : means just for the current WorkerThread transaction.
Context.AttributeScope.CONNECTION: means for the user connection.
Context.AttributeScope.CONTROLLER: for controller session

for example if I want to attach something which is valid for the lifetime of the users connection
I might do:

WorkerThread workerThread = (WorkerThread) Thread.currentThread();
AttributeHolder connectionAttrs =
                ctx.getAttributeHolderByScope(Context.AttributeScope.CONNECTION);
if (connectionAttrs == null) {
            connectionAttrs = workerThread.getAttachment();
            ctx.getSelectionKey().attach(connectionAttrs);
}
connectionAttrs.setAttribute("myKey", userData);



Many Greetings John






-- 
GMX Kostenlose Spiele: Einfach online spielen und Spaß haben mit Pastry Passion!
http://games.entertainment.gmx.net/de/entertainment/games/free/puzzle/6169196