Hello John,
great!
Just one small comment, to avoid using "instance of" it's possible to
do following in broadcastMessage:
AttributeHolder connectionAttrs =
ctx.getAttributeHolderByScope(Context.AttributeScope.CONNECTION);
if (connectionAttrs != null) {
Integer p = (Integer) connectionAttrs.getAttribute(PROJECT_ID);
if (p == null) continue;
// write something to key
}
Otherwise code look good!
Thanks.
WBR,
Alexey.
On May 22, 2008, at 19:40 , John ROM wrote:
>
> Hi Oleksiy,
>
> thank you very much!!
>
> Just for the record:
>
> //setting an attribute on a connection:
>
> public void attachIdToConnection(int id) {
> WorkerThread workerThread = (WorkerThread)
> Thread.currentThread();
> // Detach the current Thread data.
>
> AttributeHolder connectionAttrs =
>
> ctx.getAttributeHolderByScope(Context.AttributeScope.CONNECTION);
> if (connectionAttrs == null) {
> connectionAttrs =
> workerThread.getAttachment();
> ctx.getSelectionKey().attach(connectionAttrs);
> }
> connectionAttrs.setAttribute("PROJECT_ID", id);
>
> // Attach it to the SelectionKey so the it can be resumed
> latter.
>
> }
>
> // usecase broadcasting:
>
> public void broadcastMessage( int project) throws Exception {
>
> Set<SelectionKey> selectionKeys =
> ctx.getSelectorHandler().keys();
> for (SelectionKey key : selectionKeys) {
>
>
> if (key.isValid()) {
> if (key == ctx.getSelectionKey()) continue;
> Object attachment = key.attachment();
> if (attachment instanceof AttributeHolder) {
> AttributeHolder connectionAttrs =
> (AttributeHolder) attachment;
>
> Integer p = (Integer)
> connectionAttrs.getAttribute(PROJECT_ID);
> if (p == null) continue;
> // write something to key
>
> }
> }
>
> }
>
>
> }
>
>
>
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: Oleksiy Stashok [mailto:Oleksiy.Stashok_at_Sun.COM]
> Gesendet: Donnerstag, 22. Mai 2008 17:57
> An: users_at_grizzly.dev.java.net
> Betreff: Re: How-to attach an ID to a Connection?
>
> Hi John,
>
> IMHO right thing to do - is to store Attributes in ThreadAttachment.
> You can set it directly using ThreadAttachment or
> ctx.getAttributeHolderByScope(AttributeScope.CONNECTION).
>
> It shouldn't be overwritten :)
>
> Thanks.
>
> WBR,
> Alexey.
>
> On May 22, 2008, at 17:45 , John ROM wrote:
>
>> I'm only using the basic grizzly framework module and building an
>> server.
>>
>> I have selectionKeys which (hopefully) often last 12 hours.
>>
>> With each connection I need to store an Integer (project_id).
>>
>> This Integer will then be retrieved with code like:
>> //iterate over all existings connections and find
>> // keys with specific ids.
>>
>> selector.selectNow()
>>
>> Set<SelectionKey> selectionKeys = tCPSelectorHandler.keys();
>> for (SelectionKey key : selectionKeys) {
>> if(key.valid()
>> // Get Integer out of maybe ThreadAttachment
>> key.attachment();
>> }
>> }
>>
>>
>> I am a little afraid that Grizzly might overwrite the attachment
>> in my keys.
>>
>> Maybe its better to store the integers somewhere else together with
>> selectorKey.hashcode() and hope that two keys will never have the
>> same
>> hashcodes...
>>
>> So....
>>
>> What is the best way to attach something to an selectorKey or in
>> other
>> words what is the pendant to
>> AttributeHolder connectionAttrs =
>>
>> ctx.getAttributeHolderByScope(AttributeScope.CONNECTION);
>>
>>
>> Many Greetings John
>>
>>
>>
>
>
>
> --
> GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
> Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>