1 17 package org.apache.servicemix.components.jms; 18 19 import javax.jbi.JBIException; 20 import javax.jms.Connection ; 21 import javax.jms.ConnectionFactory ; 22 import javax.jms.Destination ; 23 import javax.jms.JMSException ; 24 import javax.jms.MessageConsumer ; 25 import javax.jms.Session ; 26 27 import org.springframework.beans.factory.InitializingBean; 28 import org.springframework.jms.core.JmsTemplate; 29 30 36 public class JmsReceiverComponent extends JmsInBinding implements InitializingBean { 37 private JmsTemplate template; 38 private String selector; 39 private MessageConsumer consumer; 40 private ConnectionFactory connectionFactory; 41 private Connection connection; 42 private Session session; 43 44 public void afterPropertiesSet() throws Exception { 45 if (template == null) { 46 throw new IllegalArgumentException ("Must have a template set"); 47 } 48 } 49 50 public void start() throws JBIException { 51 super.start(); 53 try { 54 connectionFactory = template.getConnectionFactory(); 55 60 if (template instanceof org.springframework.jms.core.JmsTemplate102) { 61 if (template.isPubSubDomain()) { 63 javax.jms.TopicConnection tc; 64 connection = tc = ((javax.jms.TopicConnectionFactory )connectionFactory).createTopicConnection(); 65 session = tc.createTopicSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode()); 66 } 67 else { 68 javax.jms.QueueConnection qc; 69 connection = qc = ((javax.jms.QueueConnectionFactory )connectionFactory).createQueueConnection(); 70 session = qc.createQueueSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode()); 71 } 72 } else { connection = connectionFactory.createConnection(); 74 session = connection.createSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode()); 75 } 76 77 Destination defaultDestination = template.getDefaultDestination(); 78 if (defaultDestination == null) { 79 defaultDestination = template.getDestinationResolver().resolveDestinationName(session, template.getDefaultDestinationName(), 80 template.isPubSubDomain()); 81 } 82 83 87 if (template instanceof org.springframework.jms.core.JmsTemplate102) { 88 if (template.isPubSubDomain()) { 91 consumer = ((javax.jms.TopicSession )session).createSubscriber((javax.jms.Topic )defaultDestination, selector, template.isPubSubNoLocal()); 92 } else { 93 consumer = ((javax.jms.QueueSession )session).createReceiver((javax.jms.Queue )defaultDestination, selector); 94 } 95 } else { consumer = session.createConsumer(defaultDestination, selector); 97 } 98 connection.start(); 99 consumer.setMessageListener(this); 100 } catch (JMSException e) { 101 throw new JBIException("Unable to start jms component"); 102 } 103 } 104 105 public void stop() throws JBIException { 106 try { 107 if (consumer != null) { 108 consumer.close(); 109 } 110 if (session != null) { 111 session.close(); 112 } 113 if (connection != null) { 114 connection.close(); 115 } 116 } catch (JMSException e) { 117 throw new JBIException("Unable to stop jms component"); 118 } finally { 119 connection = null; 120 session = null; 121 consumer = null; 122 } 123 } 124 125 public JmsTemplate getTemplate() { 126 return template; 127 } 128 129 public void setTemplate(JmsTemplate template) { 130 this.template = template; 131 } 132 133 public String getSelector() { 134 return selector; 135 } 136 137 public void setSelector(String selector) { 138 this.selector = selector; 139 } 140 141 } 142 143 | Popular Tags |