1 17 package org.apache.log.output.jms; 18 19 import javax.jms.Message ; 20 import javax.jms.Session ; 21 import javax.jms.Topic ; 22 import javax.jms.TopicConnection ; 23 import javax.jms.TopicConnectionFactory ; 24 import javax.jms.TopicPublisher ; 25 import javax.jms.TopicSession ; 26 import org.apache.log.ErrorHandler; 27 28 33 public class JMSTopicTarget 34 extends AbstractJMSTarget 35 { 36 private TopicConnectionFactory m_factory; 38 39 private Topic m_topic; 41 42 private TopicSession m_session; 44 45 private TopicPublisher m_publisher; 47 48 private TopicConnection m_connection; 50 51 public JMSTopicTarget( final MessageBuilder builder, 52 final TopicConnectionFactory factory, 53 final Topic 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 factory, 63 final Topic 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 message ) 73 { 74 try 75 { 76 m_publisher.publish( message ); 77 } 78 catch( final Exception e ) 79 { 80 getErrorHandler().error( "Error publishing message", e, null ); 81 } 82 } 83 84 protected Session 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 } 105 catch( final Exception 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 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 |