List of usage examples for javax.jms TopicSubscriber setMessageListener
void setMessageListener(MessageListener listener) throws JMSException;
From source file:org.wso2.carbon.appfactory.eventing.jms.Subscriber.java
public void subscribe() throws AppFactoryEventException { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, ANDES_ICF); properties.put(CF_NAME_PREFIX + CF_NAME, Util.getTCPConnectionURL()); properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true"); properties.put("topic", topicName); try {/*from w ww .ja v a 2 s .c om*/ ctx = new InitialContext(properties); // Lookup connection factory connFactory = (TopicConnectionFactory) ctx.lookup(CF_NAME); topicConnection = connFactory.createTopicConnection(); TopicSession topicSession = topicConnection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE); // create durable subscriber with subscription ID Topic topic = null; try { topic = (Topic) ctx.lookup(topicName); } catch (NamingException e) { topic = topicSession.createTopic(topicName); } TopicSubscriber topicSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId); topicSubscriber.setMessageListener(messageListener); topicConnection.start(); } catch (NamingException e) { String errorMsg = "Failed to subscribe to topic:" + topicName + " due to " + e.getMessage(); throw new AppFactoryEventException(errorMsg, e); } catch (JMSException e) { String errorMsg = "Failed to subscribe to topic:" + topicName + " due to " + e.getMessage(); throw new AppFactoryEventException(errorMsg, e); } }
From source file:org.wso2.carbon.event.core.internal.delivery.jms.JMSDeliveryManager.java
public void subscribe(Subscription subscription) throws EventBrokerException { if (isDeactivated()) { return;//ww w . j av a 2 s .c om } // in a multi tenant envirionment deployment synchronizer may creates subscriptions before // the event observer get activated. if (this.subscriptionIDSessionDetailsMap.containsKey(subscription.getId())) { log.warn( "There is an subscription already exists for the subscription with id " + subscription.getId()); return; } JMSMessageListener jmsMessageListener = new JMSMessageListener(this.notificationManager, subscription); try { TopicConnection topicConnection = getTopicConnection(subscription.getOwner()); TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); String topicName = ""; if (subscription.getTenantDomain() != null && (!subscription.getTenantDomain() .equals(org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME))) { if (!subscription.getTopicName().startsWith("/")) { topicName = getTopicName(subscription.getTenantDomain() + "/" + subscription.getTopicName()); } else { topicName = getTopicName(subscription.getTenantDomain() + subscription.getTopicName()); } } else { topicName = getTopicName(subscription.getTopicName()); } Topic topic = topicSession.createTopic(topicName); //Some times we are not getting the proper topic with the required syntax, if it is not //appropriate we need to check and add the BURL syntax to fix the issue https://wso2.org/jira/browse/MB-185 if (!topic.toString().startsWith("topic://amq.topic")) { topic = topicSession.createTopic("BURL:" + topicName); } TopicSubscriber topicSubscriber = topicSession.createDurableSubscriber(topic, subscription.getId()); topicSubscriber.setMessageListener(jmsMessageListener); this.subscriptionIDSessionDetailsMap.put(subscription.getId(), new JMSSubscriptionDetails(topicSubscriber, topicSession, topicConnection)); } catch (JMSException e) { throw new EventBrokerException( "Can not subscribe to topic " + subscription.getTopicName() + " " + e.getMessage(), e); } }
From source file:org.wso2.carbon.andes.event.core.internal.delivery.jms.JMSDeliveryManager.java
/** * {@inheritDoc}/* w w w . ja v a 2s. com*/ */ public void subscribe(Subscription subscription) throws EventBrokerException { if (isDeactivated()) { return; } // in a multi tenant environment deployment synchronize may creates subscriptions before // the event observer get activated. if (this.subscriptionIDSessionDetailsMap.containsKey(subscription.getId())) { log.warn( "There is an subscription already exists for the subscription with id " + subscription.getId()); return; } JMSMessageListener jmsMessageListener = new JMSMessageListener(this.notificationManager, subscription); try { TopicConnection topicConnection = getTopicConnection(subscription.getOwner()); TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); String topicName = ""; if (subscription.getTenantDomain() != null && (!subscription.getTenantDomain() .equals(org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME))) { if (!subscription.getTopicName().startsWith("/")) { topicName = getTopicName(subscription.getTenantDomain() + "/" + subscription.getTopicName()); } else { topicName = getTopicName(subscription.getTenantDomain() + subscription.getTopicName()); } } else { topicName = getTopicName(subscription.getTopicName()); } Topic topic = topicSession.createTopic(topicName); //Some times we are not getting the proper topic with the required syntax, if it is not //appropriate we need to check and add the BURL syntax to fix the issue https://wso2.org/jira/browse/MB-185 if (!topic.toString().startsWith("topic://amq.topic")) { topic = topicSession.createTopic("BURL:" + topicName); } TopicSubscriber topicSubscriber = topicSession.createDurableSubscriber(topic, subscription.getId()); topicSubscriber.setMessageListener(jmsMessageListener); this.subscriptionIDSessionDetailsMap.put(subscription.getId(), new JMSSubscriptionDetails(topicSubscriber, topicSession, topicConnection)); } catch (JMSException e) { throw new EventBrokerException( "Can not subscribe to topic " + subscription.getTopicName() + " " + e.getMessage(), e); } }
From source file:org.openmrs.event.EventEngine.java
/** * @see Event#subscribe(Destination, EventListener) */// w w w. ja v a2s . c o m public void subscribe(Destination destination, final EventListener listenerToRegister) { initializeIfNeeded(); TopicConnection conn; Topic topic = (Topic) destination; try { conn = (TopicConnection) jmsTemplate.getConnectionFactory().createConnection(); TopicSession session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); TopicSubscriber subscriber = session.createSubscriber(topic); subscriber.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { listenerToRegister.onMessage(message); } }); //Check if this is a duplicate and remove it String key = topic.getTopicName() + DELIMITER + listenerToRegister.getClass().getName(); if (subscribers.containsKey(key)) { unsubscribe(destination, listenerToRegister); } subscribers.put(key, subscriber); conn.start(); } catch (JMSException e) { // TODO Auto-generated catch block. Do something smarter here. e.printStackTrace(); } // List<EventListener> currentListeners = listeners.get(key); // // if (currentListeners == null) { // currentListeners = new ArrayList<EventListener>(); // currentListeners.add(listenerToRegister); // listeners.put(key, currentListeners); // if (log.isInfoEnabled()) // log.info("subscribed: " + listenerToRegister + " to key: " // + key); // // } else { // // prevent duplicates because of weird spring loading // String listernToRegisterName = listenerToRegister.getClass() // .getName(); // Iterator<EventListener> iterator = currentListeners.iterator(); // while (iterator.hasNext()) { // EventListener lstnr = iterator.next(); // if (lstnr.getClass().getName().equals(listernToRegisterName)) // iterator.remove(); // } // // if (log.isInfoEnabled()) // log.info("subscribing: " + listenerToRegister + " to key: " // + key); // // currentListeners.add(listenerToRegister); // } }
From source file:org.apache.stratos.adc.topology.mgt.subscriber.TopologySubscriber.java
public static void subscribe(String topicName) { Properties initialContextProperties = new Properties(); TopicSubscriber topicSubscriber = null; TopicSession topicSession = null;/* w w w. j a va2s. com*/ TopicConnection topicConnection = null; InitialContext initialContext = null; initialContextProperties.put("java.naming.factory.initial", "org.wso2.andes.jndi.PropertiesFileInitialContextFactory"); String mbServerIp = System.getProperty(TopologyConstants.MB_SERVER_IP) == null ? TopologyConstants.DEFAULT_MB_SERVER_IP : System.getProperty(TopologyConstants.MB_SERVER_IP); String connectionString = "amqp://admin:admin@clientID/carbon?brokerlist='tcp://" + mbServerIp + "'&reconnect='true'"; initialContextProperties.put("connectionfactory.qpidConnectionfactory", connectionString); try { initialContext = new InitialContext(initialContextProperties); TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) initialContext .lookup("qpidConnectionfactory"); topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = topicSession.createTopic(topicName); topicSubscriber = topicSession.createSubscriber(topic); topicSubscriber.setMessageListener(new TopologyListener()); } catch (Exception e) { log.error(e.getMessage(), e); try { if (topicSubscriber != null) { topicSubscriber.close(); } if (topicSession != null) { topicSession.close(); } if (topicConnection != null) { topicConnection.close(); } } catch (JMSException e1) { // ignore } } finally { // start the health checker Thread healthChecker = new Thread(new TopicHealthChecker(topicName, topicSubscriber)); healthChecker.start(); } }
From source file:org.okj.commons.broker.SimpleMessageSubscriber.java
/** * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() *///from w w w. j a v a2s . c o m @Override public void afterPropertiesSet() throws Exception { //bean?????? try { if (connectionFactory != null) { //1. ? this.connection = ((TopicConnectionFactory) connectionFactory).createTopicConnection(); //2. ?? this.session = this.connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //3. this.topic = session.createTopic(topicName); //4. ?? TopicSubscriber subscriber = null; if (StringUtils.isNotBlank(selector)) { subscriber = session.createSubscriber(topic, selector, false); //? } else { subscriber = session.createSubscriber(topic); } subscriber.setMessageListener(this); //2. ?? this.connection.start(); LogUtils.info(LOGGER, "???topicName={0}", topicName); } } catch (JMSException ex) { LogUtils.error(LOGGER, "??", ex); } }
From source file:org.carewebframework.jms.GlobalEventDispatcher.java
/** * Registers an event subscription with the global event manager. Note that the global event * manager has no knowledge of each event's individual subscribers - only that the event of a * given name has subscribers. This is because the global event manager need only dispatch * events to the local event manager. The local event manager will then dispatch events to the * individual subscribers.//www . j a v a2 s .c o m * * @param eventName Name of event. * @throws JMSException JMS exception. */ private void doHostSubscribe(final String eventName) throws JMSException { if (this.subscribers.get(eventName) != null) { if (log.isDebugEnabled()) { log.debug(String.format("Already subscribed to Topic[%s]", eventName)); } return; } if (log.isDebugEnabled()) { log.debug(String.format("Subscribing to Topic[%s]", eventName)); } final String topicName = JMSUtil.getTopicName(eventName); final String selector = JMSUtil.getMessageSelector(eventName, getPublisherInfo()); // This doesn't actually create a physical topic. In ActiveMQ, a topic is created on-demand when someone with the // authority to create topics submits something to a topic. By default, everyone has the authority to create topics. See // http://markmail.org/message/us7v5ocnb65m4fdp#query:createtopic%20activemq%20jms+page:1+mid:tce6soq5g7rdkqnw+state:results --lrc final Topic topic = this.session.createTopic(topicName); final TopicSubscriber subscriber = this.session.createSubscriber(topic, selector, false); this.subscribers.put(eventName, subscriber); subscriber.setMessageListener(this); }
From source file:com.zotoh.maedr.device.JmsIO.java
private void inizTopic(Context ctx, Object obj) throws Exception { TopicConnectionFactory f = (TopicConnectionFactory) obj; final JmsIO me = this; TopicConnection conn;/*from www . j a v a2 s . c om*/ Topic t = (Topic) ctx.lookup(_dest); if (!isEmpty(_jmsUser)) { conn = f.createTopicConnection(_jmsUser, _jmsPwd); } else { conn = f.createTopicConnection(); } _conn = conn; TopicSession s = conn.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); TopicSubscriber b; if (_durable) { b = s.createDurableSubscriber(t, GUID.generate()); } else { b = s.createSubscriber(t); } b.setMessageListener(new MessageListener() { public void onMessage(Message msg) { me.onMessage(msg); } }); }
From source file:org.aludratest.service.jms.impl.JmsActionImpl.java
@Override public void subscribeTopic(MessageListener listener, @TechnicalLocator String destinationName, @TechnicalArgument String messageSelector, @TechnicalArgument String subscriptionName, @TechnicalArgument boolean durable) throws JMSException { if (StringUtils.isEmpty(subscriptionName)) { throw new IllegalArgumentException("subscriptionName must be provided to subscribe!"); }//from w ww .j av a 2 s . c om Topic topic; try { topic = (Topic) context.lookup(destinationName); } catch (NamingException e) { throw new AutomationException("Could not lookup destination " + destinationName, e); } LOGGER.debug("Creating topic-subscriber for topic " + destinationName + " and subscriptionname " + subscriptionName); Connection c = getDynamicConnection(subscriptionName); TopicSession ts = (TopicSession) c.createSession(false, Session.AUTO_ACKNOWLEDGE); if (durable) { TopicSubscriber subscriber = ts.createDurableSubscriber(topic, subscriptionName, messageSelector, false); subscriber.setMessageListener(listener); this.durableConsumers.put(subscriptionName, subscriber); } else { ts.createSubscriber(topic, messageSelector, true).setMessageListener(listener); } }
From source file:org.apache.stratos.lb.endpoint.subscriber.TopologySubscriber.java
public static void subscribe(String topicName) { Properties initialContextProperties = new Properties(); TopicSubscriber topicSubscriber = null; TopicSession topicSession = null;/*from w ww . jav a2 s . co m*/ TopicConnection topicConnection = null; InitialContext initialContext = null; initialContextProperties.put("java.naming.factory.initial", "org.wso2.andes.jndi.PropertiesFileInitialContextFactory"); String mbServerUrl = null; if (ConfigHolder.getInstance().getLbConfig() != null) { mbServerUrl = ConfigHolder.getInstance().getLbConfig().getLoadBalancerConfig().getMbServerUrl(); } String connectionString = "amqp://admin:admin@clientID/carbon?brokerlist='tcp://" + (mbServerUrl == null ? TopologyConstants.DEFAULT_MB_SERVER_URL : mbServerUrl) + "'&reconnect='true'"; initialContextProperties.put("connectionfactory.qpidConnectionfactory", connectionString); try { initialContext = new InitialContext(initialContextProperties); TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) initialContext .lookup("qpidConnectionfactory"); topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = topicSession.createTopic(topicName); topicSubscriber = topicSession.createSubscriber(topic); topicSubscriber.setMessageListener(new TopologyListener()); } catch (Exception e) { log.error(e.getMessage(), e); try { if (topicSubscriber != null) { topicSubscriber.close(); } if (topicSession != null) { topicSession.close(); } if (topicConnection != null) { topicConnection.close(); } } catch (JMSException e1) { // ignore } } finally { // start the health checker Thread healthChecker = new Thread(new TopicHealthChecker(topicName, topicSubscriber)); healthChecker.start(); } }