KickJava   Java API By Example, From Geeks To Geeks.

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


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.Queue JavaDoc;
21 import javax.jms.QueueConnection JavaDoc;
22 import javax.jms.QueueConnectionFactory JavaDoc;
23 import javax.jms.QueueSender JavaDoc;
24 import javax.jms.QueueSession JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import org.apache.log.ErrorHandler;
27
28 /**
29  * A target that writes to a JMS Queue.
30  *
31  * @author <a HREF="mailto:mirceatoma@home.com">Mircea Toma</a>
32  */

33 public class JMSQueueTarget
34     extends AbstractJMSTarget
35 {
36     ///ConnectionFactory to use
37
private QueueConnectionFactory JavaDoc m_factory;
38
39     ///Queue we will send messages to
40
private Queue JavaDoc m_queue;
41
42     ///Session associated with queue
43
private QueueSession JavaDoc m_session;
44
45     ///Sender for queue
46
private QueueSender JavaDoc m_sender;
47
48     ///JMS queue Connection
49
private QueueConnection JavaDoc m_connection;
50
51     public JMSQueueTarget( final MessageBuilder builder,
52                            final QueueConnectionFactory JavaDoc factory,
53                            final Queue JavaDoc queue )
54     {
55         super( builder );
56         m_factory = factory;
57         m_queue = queue;
58         open();
59     }
60
61     public JMSQueueTarget( final MessageBuilder builder,
62                            final QueueConnectionFactory JavaDoc factory,
63                            final Queue JavaDoc queue,
64                            final ErrorHandler handler )
65     {
66         super( builder, handler );
67         m_factory = factory;
68         m_queue = queue;
69         open();
70     }
71
72     protected void send( final Message JavaDoc message )
73     {
74         try
75         {
76             m_sender.send( 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.createQueueConnection();
94             m_connection.start();
95
96             m_session =
97                 m_connection.createQueueSession( false, Session.AUTO_ACKNOWLEDGE );
98
99             m_sender = m_session.createSender( m_queue );
100         }
101         catch( final Exception JavaDoc e )
102         {
103             getErrorHandler().error( "Error starting connection", e, null );
104         }
105     }
106
107     protected synchronized void closeConnection()
108     {
109         try
110         {
111             if( null != m_sender ) m_sender.close();
112             if( null != m_session ) m_session.close();
113             if( null != m_connection ) m_connection.close();
114         }
115         catch( Exception JavaDoc e )
116         {
117             getErrorHandler().error( "Error closing connection", e, null );
118         }
119
120         m_sender = null;
121         m_session = null;
122         m_connection = null;
123     }
124 }
125
126
Popular Tags