List of usage examples for javax.jms TopicConnectionFactory createTopicConnection
TopicConnection createTopicConnection() throws JMSException;
From source file:org.wso2.carbon.sample.jmsclient.JMSClient.java
public static void publishMessages(String topicName, String broker) { try {//from w ww .j a va 2s. c o m // filePath = JMSClientUtil.getEventFilePath(sampleNumber, format, topicName, filePath); TopicConnection topicConnection = null; Session session = null; Properties properties = new Properties(); if (broker.equalsIgnoreCase("activemq")) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } else if (broker.equalsIgnoreCase("mb")) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context .lookup("qpidConnectionFactory"); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } else if (broker.equalsIgnoreCase("qpid")) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context .lookup("qpidConnectionFactory"); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } else { log.info("Please enter a valid JMS message broker. (ex: activemq, mb, qpid"); } if (session != null) { Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); try { List<Map<String, Object>> messageList = new ArrayList<Map<String, Object>>(); Random random = new Random(); for (int j = 0; j < sessionsPerThread; j++) { for (int i = 0; i < msgsPerSession; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("id", random.nextInt() + ""); map.put("value", random.nextInt()); map.put("content", "sample content"); map.put("client", "jmsQueueClient"); // setting the timestamp later messageList.add(map); } publishMapMessage(producer, session, messageList); } } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); topicConnection.stop(); } } } catch (Exception e) { log.error("Error when publishing message" + e.getMessage(), e); } }
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;//from w w w . ja v a2 s . c o m 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.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 . ja va 2 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(); } }
From source file:org.wso2.carbon.integration.test.client.JMSPublisherClient.java
/** * This method will publish the data in the test data file to the given topic via ActiveMQ message broker * * @param topicName the topic which the messages should be published under * @param format format of the test data file (csv or text) * @param testCaseFolderName Testcase folder name which is in the test artifacts folder * @param dataFileName data file name with the extension to be read * *///from www. ja v a 2 s. c o m public static void publish(String topicName, String format, String testCaseFolderName, String dataFileName) { if (format == null || "map".equals(format)) { format = "csv"; } try { Properties properties = new Properties(); String filePath = getTestDataFileLocation(testCaseFolderName, dataFileName); properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); TopicConnection topicConnection = connFactory.createTopicConnection(); topicConnection.start(); Session session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); List<String> messagesList = readFile(filePath); try { if (format.equalsIgnoreCase("csv")) { log.info("Sending Map messages on '" + topicName + "' topic"); publishMapMessages(producer, session, messagesList); } else { log.info("Sending " + format + " messages on '" + topicName + "' topic"); publishTextMessage(producer, session, messagesList); } } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } finally { producer.close(); session.close(); topicConnection.stop(); topicConnection.close(); } } catch (Exception e) { log.error("Error when publishing messages" + e.getMessage(), e); } log.info("All Order Messages sent"); }
From source file:org.wso2.carbon.bpmn.extensions.jms.JMSUtils.java
/** * * @param connectionFactory/*w ww . ja va 2s . c om*/ * @param username * @param password * @param isQueue * @return */ public static Connection createConnection(ConnectionFactory connectionFactory, String username, String password, Boolean isQueue) throws JMSException { Connection connection = null; if (isQueue == null) { if (username == null && password == null) { connection = connectionFactory.createConnection(); } else { connection = connectionFactory.createConnection(username, password); } } else { QueueConnectionFactory queueConnectionFactory = null; TopicConnectionFactory topicConnectionFactory = null; if (isQueue) { queueConnectionFactory = QueueConnectionFactory.class.cast(connectionFactory); } else { topicConnectionFactory = TopicConnectionFactory.class.cast(connectionFactory); } if (queueConnectionFactory != null) { if (username == null && password == null) { connection = queueConnectionFactory.createQueueConnection(); } else { connection = queueConnectionFactory.createQueueConnection(username, password); } } else if (topicConnectionFactory != null) { if (username == null && password == null) { connection = topicConnectionFactory.createTopicConnection(); } else { connection = topicConnectionFactory.createTopicConnection(username, password); } } } return connection; }
From source file:org.apache.stratos.autoscaler.integration.TopicPublisher.java
public void connect() throws NamingException, JMSException, IOException { // Prepare JNDI properties Properties properties = new Properties(); properties.put("java.naming.provider.url", "tcp://localhost:61616"); properties.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); properties.put("connectionfactoryName", "ConnectionFactory"); properties.put("java.naming.security.principal", "admin"); properties.put("java.naming.security.credentials", "admin"); InitialContext ctx = new InitialContext(properties); // Lookup connection factory String connectionFactoryName = properties.get("connectionfactoryName").toString(); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) ctx.lookup(connectionFactoryName); topicConnection = connectionFactory.createTopicConnection(); topicConnection.start();/* w w w .j a v a2 s .com*/ topicSession = topicConnection.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE); // Create topic topic = topicSession.createTopic(topicName); }
From source file:org.springframework.jms.connection.ConnectionFactoryUtils.java
/** * Obtain a JMS TopicSession that is synchronized with the current transaction, if any. * <p>Mainly intended for use with the JMS 1.0.2 API. * @param cf the ConnectionFactory to obtain a Session for * @param existingCon the existing JMS Connection to obtain a Session for * (may be {@code null})/*from w w w . j av a2 s . c o m*/ * @param synchedLocalTransactionAllowed whether to allow for a local JMS transaction * that is synchronized with a Spring-managed transaction (where the main transaction * might be a JDBC-based one for a specific DataSource, for example), with the JMS * transaction committing right after the main transaction. If not allowed, the given * ConnectionFactory needs to handle transaction enlistment underneath the covers. * @return the transactional Session, or {@code null} if none found * @throws JMSException in case of JMS failure */ @Nullable public static TopicSession getTransactionalTopicSession(final TopicConnectionFactory cf, @Nullable final TopicConnection existingCon, final boolean synchedLocalTransactionAllowed) throws JMSException { return (TopicSession) doGetTransactionalSession(cf, new ResourceFactory() { @Override @Nullable public Session getSession(JmsResourceHolder holder) { return holder.getSession(TopicSession.class, existingCon); } @Override @Nullable public Connection getConnection(JmsResourceHolder holder) { return (existingCon != null ? existingCon : holder.getConnection(TopicConnection.class)); } @Override public Connection createConnection() throws JMSException { return cf.createTopicConnection(); } @Override public Session createSession(Connection con) throws JMSException { return ((TopicConnection) con).createTopicSession(synchedLocalTransactionAllowed, Session.AUTO_ACKNOWLEDGE); } @Override public boolean isSynchedLocalTransactionAllowed() { return synchedLocalTransactionAllowed; } }, true); }
From source file:org.nuxeo.ecm.core.event.jms.JmsEventForwarder.java
protected void produceJMSMessage(SerializableEventBundle message) throws JMSBusNotActiveException { InitialContext ctx;// w ww .j av a 2 s .c o m Topic nuxeoTopic; try { ctx = new InitialContext(); nuxeoTopic = (Topic) ctx.lookup(NUXEO_JMS_TOPIC); } catch (NamingException e) { jmsBusIsActive = false; throw new JMSBusNotActiveException(e); } TopicConnection nuxeoTopicConnection = null; TopicSession nuxeoTopicSession = null; TopicPublisher nuxeoMessagePublisher = null; try { TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory"); nuxeoTopicConnection = factory.createTopicConnection(); nuxeoTopicSession = nuxeoTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); ObjectMessage jmsMessage = nuxeoTopicSession.createObjectMessage(message); // add Headers for JMS message jmsMessage.setStringProperty("BundleEvent", message.getEventBundleName()); nuxeoMessagePublisher = nuxeoTopicSession.createPublisher(nuxeoTopic); nuxeoMessagePublisher.send(jmsMessage); log.debug("Event bundle " + message.getEventBundleName() + " forwarded to JMS topic"); } catch (Exception e) { log.error("Error during JMS forwarding", e); } finally { if (nuxeoTopicSession != null) { try { if (nuxeoMessagePublisher != null) { nuxeoMessagePublisher.close(); } nuxeoTopicConnection.close(); nuxeoTopicSession.close(); } catch (JMSException e) { log.error("Error during JMS cleanup", e); } } } }
From source file:org.wso2.mb.integration.common.clients.operations.topic.TopicMessagePublisher.java
public TopicMessagePublisher(String connectionString, String hostName, String port, String userName, String password, String topicName, AtomicInteger messageCounter, int numOfMessagesToSend, int delayBetweenMessages, String filePath, int printNumberOfMessagesPer, boolean isToPrintEachMessage, long jmsExpiration) { this.hostName = hostName; this.port = port; this.connectionString = connectionString; this.messageCounter = messageCounter; this.topicName = topicName; this.numOfMessagesToSend = numOfMessagesToSend; this.delay = delayBetweenMessages; this.filePath = filePath; if (filePath != null && !filePath.equals("")) { readFromFile = true;/*from w w w . ja v a2 s .c o m*/ } this.printNumberOfMessagesPer = printNumberOfMessagesPer; this.isToPrintEachMessage = isToPrintEachMessage; this.jmsExpiration = jmsExpiration; Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF); properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password)); properties.put("topic." + topicName, topicName); try { InitialContext ctx = new InitialContext(properties); // Lookup connection factory TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx.lookup(CF_NAME); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); topicSession = topicConnection.createTopicSession(true, TopicSession.SESSION_TRANSACTED); // Send message Topic topic = (Topic) ctx.lookup(topicName); topicPublisher = topicSession.createPublisher(topic); } catch (NamingException e) { log.error("Error while looking up for topic", e); } catch (JMSException e) { log.error("Error while initializing topic connection", e); } }
From source file:org.wso2.mb.integration.common.clients.operations.topic.TopicMessageReceiver.java
public TopicMessageReceiver(String connectionString, String hostName, String port, String userName, String password, String topicName, boolean isDurable, String subscriptionID, int ackMode, boolean useMessageListener, AtomicInteger messageCounter, int delayBetweenMessages, int printNumberOfMessagesPer, boolean isToPrintEachMessage, String fileToWriteReceivedMessages, int stopAfter, int unsubscrbeAfter, int ackAfterEach, int commitAfterEach, int rollbackAfterEach) { this.hostName = hostName; this.port = port; this.connectionString = connectionString; this.useMessageListener = useMessageListener; this.delayBetweenMessages = delayBetweenMessages; this.messageCounter = messageCounter; this.topicName = topicName; this.printNumberOfMessagesPer = printNumberOfMessagesPer; this.isToPrintEachMessage = isToPrintEachMessage; this.fileToWriteReceivedMessages = fileToWriteReceivedMessages; this.stopAfter = stopAfter; this.unSubscribeAfter = unsubscrbeAfter; this.ackAfterEach = ackAfterEach; this.commitAfterEach = commitAfterEach; this.rollbackAfterEach = rollbackAfterEach; this.subscriptionId = subscriptionID; Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF); properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password)); properties.put("topic." + topicName, topicName); log.info("getTCPConnectionURL(userName,password) = " + getTCPConnectionURL(userName, password)); try {//from w w w. j a v a 2 s. c om InitialContext ctx = new InitialContext(properties); // Lookup connection factory TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx.lookup(CF_NAME); topicConnection = connFactory.createTopicConnection(); topicConnection.setClientID(subscriptionId); topicConnection.start(); if (ackMode == TopicSession.SESSION_TRANSACTED) { topicSession = topicConnection.createTopicSession(true, QueueSession.SESSION_TRANSACTED); } else { topicSession = topicConnection.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE); } // Send message Topic topic = (Topic) ctx.lookup(topicName); log.info("Starting listening on topic: " + topic); if (isDurable) { topicSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId); } else { topicSubscriber = topicSession.createSubscriber(topic); } } catch (NamingException e) { log.error("Error while looking up for topic", e); } catch (JMSException e) { log.error("Error while initializing topic connection", e); } }