KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > client > unicast > UnicastClientSession


1 package com.ubermq.jms.client.unicast;
2
3 import com.ubermq.jms.client.*;
4 import com.ubermq.jms.client.impl.*;
5 import com.ubermq.kernel.*;
6 import java.io.*;
7 import java.nio.channels.*;
8
9 /**
10  * This client session extends the abstract base client session to make socket
11  * connections to internet destinations.
12  * This client session uses direct point to point socket connections.
13  */

14 public class UnicastClientSession
15     extends AbstractClientSession
16     implements IClientSession
17 {
18     private IDatagramFactory factory;
19
20     /**
21      * Creates a session with the given datagram factory to handle processing.
22      * @param f the datagram factory to use.
23      */

24     public UnicastClientSession(IDatagramFactory f)
25         throws java.io.IOException JavaDoc
26     {
27         super();
28         this.factory = f;
29     }
30
31     public IConnectionInfo connect(javax.jms.Connection JavaDoc cxn,
32                                    ConnectionDescriptor descriptor,
33                                    IMessageProcessor proc)
34         throws java.io.IOException JavaDoc
35     {
36         // connect
37
InternetConnectionDescriptor d = (InternetConnectionDescriptor)descriptor;
38         SocketChannel sc = SocketChannel.open();
39
40         // we can do more sophisticated things if
41
// the connection descriptor is also an overflow
42
// handler.
43
if (descriptor instanceof IOverflowHandler)
44         {
45             IOverflowHandler overflow = (IOverflowHandler)descriptor;
46
47             while(true)
48             {
49                 try {
50                     sc.connect(d.getAddress());
51                     break;
52                 } catch(IOException iox) {
53                     if (overflow.overflow() == IOverflowHandler.ACTION_FAIL) {
54                         throw iox;
55                     } else {
56                         overflow = overflow.getRetryHandler();
57
58                         // reset the socket channel to a fresh one.
59
sc = SocketChannel.open();
60                     }
61                 }
62             }
63         } else {
64             sc.connect(d.getAddress());
65         }
66
67         // configure the socket.
68
sc.socket().setTcpNoDelay(true);
69
70         // return the connection info
71
ConnectionInfo i = new SocketChannelConnectionInfo(sc, factory, proc);
72         proc.accept(i);
73         return i;
74     }
75 }
76
Popular Tags