1 17 package org.apache.log.output.jms; 18 19 import javax.jms.Message ; 20 import javax.jms.Queue ; 21 import javax.jms.QueueConnection ; 22 import javax.jms.QueueConnectionFactory ; 23 import javax.jms.QueueSender ; 24 import javax.jms.QueueSession ; 25 import javax.jms.Session ; 26 import org.apache.log.ErrorHandler; 27 28 33 public class JMSQueueTarget 34 extends AbstractJMSTarget 35 { 36 private QueueConnectionFactory m_factory; 38 39 private Queue m_queue; 41 42 private QueueSession m_session; 44 45 private QueueSender m_sender; 47 48 private QueueConnection m_connection; 50 51 public JMSQueueTarget( final MessageBuilder builder, 52 final QueueConnectionFactory factory, 53 final Queue 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 factory, 63 final Queue 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 message ) 73 { 74 try 75 { 76 m_sender.send( 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.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 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 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 |