KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.ubermq.jms.client.unicast;
2
3 import java.io.*;
4
5 import EDU.oswego.cs.dl.util.concurrent.*;
6
7 import com.ubermq.jms.client.*;
8 import com.ubermq.jms.client.impl.*;
9 import com.ubermq.jms.client.proc.*;
10 import com.ubermq.jms.common.datagram.*;
11 import com.ubermq.jms.common.datagram.impl.*;
12 import com.ubermq.kernel.*;
13
14 /**
15  * Implements a point-to-point topic connection. Includes connection
16  * keep-alive and reconnection functionality.
17  */

18 public class UnicastConnection
19     extends Connection
20 {
21     private static ClockDaemon cd;
22
23     private static final String JavaDoc keepAliveTopic = Configurator.getProperty(ClientConfig.UNICAST_KEEP_ALIVE,
24                                                                           ".com.ubermq.keepalive");
25
26     // instance variables for heartbeating
27
private Object JavaDoc keepAliveClockTask;
28     private javax.jms.TopicSession JavaDoc keepAliveSession;
29     private javax.jms.TopicPublisher JavaDoc keepAlivePublisher;
30
31     static {
32         cd = new ClockDaemon();
33         cd.setThreadFactory(new ThreadFactory() {
34                     public Thread JavaDoc newThread(Runnable JavaDoc r) {
35                         Thread JavaDoc t = new Thread JavaDoc(r, "UnicastTopicConnection Keep-Alive");
36                         t.setDaemon(true);
37                         return t;
38                     }
39
40                 });
41     }
42
43     /**
44      * Creates a point-to-point topic connection using
45      * a custom connection descriptor, using the default datagram
46      * factory and delivery manager.<P>
47      *
48      * @param icd a description of the connection to be made.
49      */

50     public UnicastConnection(InternetConnectionDescriptor icd)
51         throws IOException
52     {
53         this(new UnicastClientSession(DatagramFactory.getInstance()),
54              icd,
55              DatagramFactory.getHolder(),
56              new SimpleDeliveryManager());
57     }
58
59     /**
60      * Creates a point-to-point topic connection to the specified
61      * host and port, using specified datagram factories and delivery manager.<p>
62      *
63      * @param session a client session
64      * @param icd a connection descriptor
65      * @param holder the datagram factory holder
66      * @param manager the delivery manager to use
67      */

68     public UnicastConnection(IClientSession session,
69                              InternetConnectionDescriptor icd,
70                              DatagramFactoryHolder holder,
71                              IDeliveryManager manager)
72         throws IOException
73     {
74         super(session,
75               new ClientProc(holder.controlFactory()),
76               manager,
77               holder,
78               icd);
79     }
80
81     /**
82      * Starts a heartbeat. The heartbeat will cause JMSIOException's
83      * to get forwarded to the exception handler if the connection goes down.
84      * In order to perform an automatic reconnection, call <br>
85      * <code>c.setExceptionListener(new ReconnectExceptionListener(c))</code>
86      * to enable the reconnect logic.
87      * @param keepAlivePeriod heartbeat interval, in milliseconds
88      */

89     public void startHeartbeat(int keepAlivePeriod)
90     {
91         if (keepAlivePeriod > 0) {
92             try {
93                 keepAliveSession = createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
94                 keepAlivePublisher = keepAliveSession.createPublisher(keepAliveSession.createTopic(keepAliveTopic));
95                 final javax.jms.Message JavaDoc m = keepAliveSession.createMessage();
96
97                 keepAliveClockTask = cd.executePeriodically(keepAlivePeriod, new Runnable JavaDoc() {
98                             public void run()
99                             {
100                                 try
101                                 {
102                                     keepAlivePublisher.publish(m);
103                                 }
104                                 catch (javax.jms.JMSException JavaDoc e) {}
105                             }
106                         }, false);
107             }
108             catch(javax.jms.JMSException JavaDoc jmse) {}
109
110         }
111     }
112
113     /**
114      * Stops the heartbeat to keep connections alive.
115      */

116     public void stopHeartbeat()
117     {
118         ClockDaemon.cancel(keepAliveClockTask);
119         try
120         {
121             keepAlivePublisher.close();
122             keepAliveSession.close();
123         }
124         catch (javax.jms.JMSException JavaDoc e) {}
125     }
126 }
127
Popular Tags