List of usage examples for javax.jms TopicSession createTopic
Topic createTopic(String topicName) throws JMSException;
From source file:eu.eubrazilcc.lvl.storage.activemq.ActiveMQConnector.java
public void subscribe(final String topicName, final MessageListener listener) { String topicName2 = null;/*from w w w. j a v a 2 s.co m*/ checkArgument(isNotBlank(topicName2 = trimToEmpty(topicName)), "Uninitialized or invalid topic"); checkNotNull(listener); TopicConnection conn = null; TopicSession session = null; MessageConsumer consumer = null; try { conn = broker().getConsumersConnFactory().createTopicConnection(); conn.start(); session = conn.createTopicSession(false, AUTO_ACKNOWLEDGE); final Topic topic = session.createTopic(topicName2); consumer = session.createConsumer(topic); consumer.setMessageListener(listener); register(TopicSubscriber.builder().topicName(topicName2).connection(conn).session(session) .consumer(consumer).build()); LOGGER.info("Subscribed to topic: " + topicName2); } catch (JMSException e) { if (consumer != null) { try { consumer.close(); } catch (JMSException ignore) { } } if (session != null) { try { session.close(); } catch (JMSException ignore) { } } if (conn != null) { try { conn.close(); } catch (JMSException ignore) { } } LOGGER.error("Failed to subscribe to topic: " + topicName2, e); } }
From source file:eu.eubrazilcc.lvl.storage.activemq.ActiveMQConnector.java
public void sendMessage(final String topicName, final String message) { checkArgument(isNotBlank(topicName), "Uninitialized or invalid topic"); checkArgument(isNotBlank(message), "Uninitialized or invalid message"); TopicConnection conn = null;/*from w w w. j a v a 2s.c om*/ TopicSession session = null; MessageProducer producer = null; try { conn = (TopicConnection) broker().getProducersConnFactory().createConnection(); /* conn = broker().getConnFactory().createTopicConnection(); conn.start(); */ session = conn.createTopicSession(false, AUTO_ACKNOWLEDGE); final Topic topic = session.createTopic(topicName); producer = session.createProducer(topic); producer.setDeliveryMode(NON_PERSISTENT); final TextMessage textMessage = session.createTextMessage(message); producer.send(textMessage); } catch (JMSException e) { LOGGER.error("Failed to send message to topic: " + topicName, e); } finally { if (producer != null) { try { producer.close(); } catch (JMSException ignore) { } } if (session != null) { try { session.close(); } catch (JMSException ignore) { } } /* if (conn != null) { try { conn.close(); } catch (JMSException ignore) { } } */ } }
From source file:nl.nn.adapterframework.jms.JMSFacade.java
public Destination getDestination() throws NamingException, JMSException, JmsException { if (destination == null) { String destinationName = getDestinationName(); if (StringUtils.isEmpty(destinationName)) { throw new NamingException("no destinationName specified"); }//from ww w. ja v a 2 s . c om if (isLookupDestination()) { if (!useTopicFunctions || getPersistent()) { destination = getDestination(destinationName); } else { TopicSession session = null; try { session = (TopicSession) createSession(); destination = session.createTopic(destinationName); } finally { closeSession(session); } } } else { destination = getJmsMessagingSource().createDestination(destinationName); } if (destination == null) { throw new NamingException("cannot get Destination from [" + destinationName + "]"); } } return destination; }
From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java
private void produceExpiredAndOneNonExpiredMessages() throws JMSException { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName); TopicConnection connection = connectionFactory.createTopicConnection(); TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); producer.setTimeToLive(TimeUnit.SECONDS.toMillis(1)); for (int i = 0; i < 40000; i++) { sendRandomMessage(session, producer); }//w w w. j a v a 2 s. c o m producer.setTimeToLive(TimeUnit.DAYS.toMillis(1)); sendRandomMessage(session, producer); connection.close(); LOG.info("produceExpiredAndOneNonExpiredMessages done"); }
From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java
private void registerDurableSubscription() throws JMSException { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName); TopicConnection connection = connectionFactory.createTopicConnection(); connection.setClientID(clientID);//from w ww. j ava2 s. c o m TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = topicSession.createTopic(topicName); TopicSubscriber durableSubscriber = topicSession.createDurableSubscriber(topic, durableSubName); connection.start(); durableSubscriber.close(); connection.close(); LOG.info("Durable Sub Registered"); }
From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java
private Message collectMessagesFromDurableSubscriptionForOneMinute() throws Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName); TopicConnection connection = connectionFactory.createTopicConnection(); connection.setClientID(clientID);/* ww w. j av a2 s. c o m*/ TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = topicSession.createTopic(topicName); connection.start(); TopicSubscriber subscriber = topicSession.createDurableSubscriber(topic, durableSubName); LOG.info("About to receive messages"); Message message = subscriber.receive(120000); subscriber.close(); connection.close(); LOG.info("collectMessagesFromDurableSubscriptionForOneMinute done"); return message; }
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; 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 {/*from w ww .ja v a 2 s .c om*/ 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.apache.stratos.lb.endpoint.subscriber.TopologySubscriber.java
public static void subscribe(String topicName) { Properties initialContextProperties = new Properties(); TopicSubscriber topicSubscriber = null; TopicSession topicSession = null; TopicConnection topicConnection = null; InitialContext initialContext = null; initialContextProperties.put("java.naming.factory.initial", "org.wso2.andes.jndi.PropertiesFileInitialContextFactory"); String mbServerUrl = null;/* w w w.j a v a2 s. co m*/ 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(); } }
From source file:org.apache.stratos.messaging.broker.connect.amqp.AmqpTopicPublisher.java
/** * Publish message to message broker.//from w w w . j a v a2 s. com * * @param message Message to be published * @param retry Retry if message broker is not available */ @Override public void publish(String message, boolean retry) { boolean published = false; while (!published) { TopicSession topicSession = null; javax.jms.TopicPublisher topicPublisher = null; try { while (connectionStatus == ConnectionStatus.ReConnecting) { // Connection has been broken, wait until reconnected try { Thread.sleep(1000); } catch (InterruptedException ignore) { } } if (connectionStatus == ConnectionStatus.ReConnected) { // Publisher has reconnected, wait another 2 seconds to make sure all subscribers // have been reconnected to receive the message Thread.sleep(2000); connectionStatus = ConnectionStatus.Connected; } topicSession = newSession(); Topic topic = lookupTopic(topicName); if (topic == null) { // if the topic doesn't exist, create it. topic = topicSession.createTopic(topicName); } topicPublisher = topicSession.createPublisher(topic); TextMessage textMessage = topicSession.createTextMessage(message); topicPublisher.publish(textMessage); published = true; } catch (Exception e) { String errorMessage = "Could not publish to topic: [topic-name] %s"; log.error(errorMessage, e); if (!retry) { // Retry is disabled, throw exception throw new MessagingException(errorMessage, e); } // Try to reconnect reconnect(); } finally { try { if (topicSession != null) { topicSession.close(); } if (topicPublisher != null) { topicPublisher.close(); } } catch (JMSException e) { message = "Error cleaning up pubisher"; log.error(message, e); throw new MessagingException(message, e); } } } }
From source file:org.apache.stratos.messaging.broker.connect.amqp.AmqpTopicSubscriber.java
@Override public void subscribe() { try {/* w ww. j a v a 2 s .co m*/ TopicSession topicSession = newSession(); Topic topic = lookupTopic(topicName); if (topic == null) { // if topic doesn't exist, create it. topic = topicSession.createTopic(topicName); } javax.jms.TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); topicSubscriber.setMessageListener(new javax.jms.MessageListener() { @Override public void onMessage(Message message) { try { String topicName = null, messageText = null; if (message instanceof ActiveMQTextMessage) { ActiveMQTextMessage textMessage = (ActiveMQTextMessage) message; topicName = textMessage.getDestination().getPhysicalName(); messageText = textMessage.getText(); } else if (message instanceof ActiveMQBytesMessage) { ActiveMQBytesMessage bytesMessage = (ActiveMQBytesMessage) message; topicName = bytesMessage.getDestination().getPhysicalName(); messageText = new String(bytesMessage.getContent().data); } else { throw new RuntimeException( String.format("Could not receive message, " + "unknown JMS message type: %s", message.getClass().getName())); } org.apache.stratos.messaging.domain.Message message_ = new org.apache.stratos.messaging.domain.Message( topicName, messageText); messageListener.messageReceived(message_); } catch (Exception e) { String error = "An error occurred when receiving message"; log.error(error, e); } } }); } catch (Exception e) { String message = "Could not subscribe to topic: " + topicName; log.error(message, e); throw new MessagingException(message, e); } }