KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > jms > JMSTopicTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.jms;
18
19 import javax.jms.Message JavaDoc;
20 import javax.jms.Session JavaDoc;
21 import javax.jms.Topic JavaDoc;
22 import javax.jms.TopicConnection JavaDoc;
23 import javax.jms.TopicConnectionFactory JavaDoc;
24 import javax.jms.TopicPublisher JavaDoc;
25 import javax.jms.TopicSession JavaDoc;
26 import org.apache.log.ErrorHandler;
27
28 /**
29  * A target that writes to a JMS Topic.
30  *
31  * @author Peter Donald
32  */

33 public class JMSTopicTarget
34     extends AbstractJMSTarget
35 {
36     ///ConnectionFactory to use
37
private TopicConnectionFactory JavaDoc m_factory;
38
39     ///Topic we will send messages to
40
private Topic JavaDoc m_topic;
41
42     ///Session associated with topic
43
private TopicSession JavaDoc m_session;
44
45     ///Publisher for topic
46
private TopicPublisher JavaDoc m_publisher;
47
48     ///JMS topic Connection
49
private TopicConnection JavaDoc m_connection;
50
51     public JMSTopicTarget( final MessageBuilder builder,
52                            final TopicConnectionFactory JavaDoc factory,
53                            final Topic JavaDoc topic )
54     {
55         super( builder );
56         m_factory = factory;
57         m_topic = topic;
58         open();
59     }
60
61     public JMSTopicTarget( final MessageBuilder builder,
62                            final TopicConnectionFactory JavaDoc factory,
63                            final Topic JavaDoc topic,
64                            final ErrorHandler handler )
65     {
66         super( builder, handler );
67         m_factory = factory;
68         m_topic = topic;
69         open();
70     }
71
72     protected void send( final Message JavaDoc message )
73     {
74         try
75         {
76             m_publisher.publish( message );
77         }
78         catch( final Exception JavaDoc e )
79         {
80             getErrorHandler().error( "Error publishing message", e, null );
81         }
82     }
83
84     protected Session JavaDoc getSession()
85     {
86         return m_session;
87     }
88
89     protected synchronized void openConnection()
90     {
91         try
92         {
93             m_connection = m_factory.createTopicConnection();
94             m_connection.start();
95
96             m_session =
97                 m_connection.createTopicSession( false, Session.AUTO_ACKNOWLEDGE );
98
99             m_publisher = m_session.createPublisher( m_topic );
100             //if( m_persistent ) publisher.setDeliveryMode( DeliveryMode.PERSISTENT );
101
//else publisher.setDeliveryMode( DeliveryMode.NON_PERSISTENT );
102
//publisher.setPriority( m_priority );
103
//publisher.setTimeToLive( m_timeToLive );
104
}
105         catch( final Exception JavaDoc e )
106         {
107             getErrorHandler().error( "Error starting connection", e, null );
108         }
109     }
110
111     protected synchronized void closeConnection()
112     {
113         try
114         {
115             if( null != m_publisher ) m_publisher.close();
116             if( null != m_session ) m_session.close();
117             if( null != m_connection ) m_connection.close();
118         }
119         catch( Exception JavaDoc e )
120         {
121             getErrorHandler().error( "Error closing connection", e, null );
122         }
123
124         m_publisher = null;
125         m_session = null;
126         m_connection = null;
127     }
128 }
129
Popular Tags