List of usage examples for javax.jms Session createQueue
Queue createQueue(String queueName) throws JMSException;
From source file:org.wso2.extension.siddhi.io.jms.source.client.JMSClient.java
public void sendJMSEvents(String filePath, String topicName, String queueName, String format, String broker, String providerURL) {//from w ww. ja v a2s. c om if (format == null || "map".equals(format)) { format = "csv"; } if ("".equalsIgnoreCase(broker)) { broker = "activemq"; } Session session = null; Properties properties = new Properties(); if (!"activemq".equalsIgnoreCase(broker) && !"mb".equalsIgnoreCase(broker) && !"qpid".equalsIgnoreCase(broker)) { log.error("Please enter a valid JMS message broker. (ex: activemq, mb, qpid"); return; } try { if (topicName != null && !"".equalsIgnoreCase(topicName)) { TopicConnection topicConnection; TopicConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); // to provide custom provider urls if (providerURL != null) { properties.put(Context.PROVIDER_URL, providerURL); } Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + topicName + "' topic"); JMSClientUtil.publishMapMessage(producer, session, messagesList); } else { log.info("Sending " + format + " messages on '" + topicName + "' topic"); JMSClientUtil.publishTextMessage(producer, session, messagesList); } log.info("All Order Messages sent"); } catch (JMSException e) { log.error("Cannot 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(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else if (queueName != null && !queueName.equalsIgnoreCase("")) { QueueConnection queueConnection; QueueConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { queueConnection = connFactory.createQueueConnection(); queueConnection.start(); session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Queue queue = session.createQueue(queueName); MessageProducer producer = session.createProducer(queue); List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + queueName + "' queue"); JMSClientUtil.publishMapMessage(producer, session, messagesList); } else { log.info("Sending " + format + " messages on '" + queueName + "' queue"); JMSClientUtil.publishTextMessage(producer, session, messagesList); } } catch (JMSException e) { log.error("Cannot subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); queueConnection.stop(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else { log.error("Enter queue name or topic name to be published!"); } } catch (Exception e) { log.error("Error when publishing" + e.getMessage(), e); } }
From source file:org.wso2.extension.siddhi.io.jms.source.client.JMSClient.java
public void sendJMSEvents(List<String> messageList, String topicName, String queueName, String format, String broker, String providerURL) { if (format == null || "map".equals(format)) { format = "csv"; }/*from w w w. j a va 2s . c om*/ if ("".equalsIgnoreCase(broker)) { broker = "activemq"; } Session session = null; Properties properties = new Properties(); if (!"activemq".equalsIgnoreCase(broker) && !"mb".equalsIgnoreCase(broker) && !"qpid".equalsIgnoreCase(broker)) { log.error("Please enter a valid JMS message broker. (ex: activemq, mb, qpid"); return; } try { if (topicName != null && !"".equalsIgnoreCase(topicName)) { TopicConnection topicConnection; TopicConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); // to provide custom provider urls if (providerURL != null) { properties.put(Context.PROVIDER_URL, providerURL); } Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); //List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + topicName + "' topic"); JMSClientUtil.publishMapMessage(producer, session, messageList); } else { log.info("Sending " + format + " messages on '" + topicName + "' topic"); JMSClientUtil.publishTextMessage(producer, session, messageList); } log.info("All Order Messages sent"); } catch (JMSException e) { log.error("Cannot 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(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else if (queueName != null && !queueName.equalsIgnoreCase("")) { QueueConnection queueConnection; QueueConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); // to provide custom provider urls if (providerURL != null) { properties.put(Context.PROVIDER_URL, providerURL); } Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { queueConnection = connFactory.createQueueConnection(); queueConnection.start(); session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Queue queue = session.createQueue(queueName); MessageProducer producer = session.createProducer(queue); //List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + queueName + "' queue"); JMSClientUtil.publishMapMessage(producer, session, messageList); } else { log.info("Sending " + format + " messages on '" + queueName + "' queue"); JMSClientUtil.publishTextMessage(producer, session, messageList); } } catch (JMSException e) { log.error("Cannot subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); queueConnection.stop(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else { log.error("Enter queue name or topic name to be published!"); } } catch (Exception e) { log.error("Error when publishing" + e.getMessage(), e); } }
From source file:org.wso2.siddhi.extension.output.transport.jms.util.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {/*from w ww. j a v a2 s.c om*/ queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages at " + queueName); while (active) { Message message = consumer.receive(1000); if (message != null) { if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message: " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message: " + ((TextMessage) message).getText()); } else { log.info("Received message: " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:test.SecureSampleApp.java
private static String sendMessage(final String encryptedMessage) { return template.execute(new SessionCallback<String>() { @Override/*from ww w . j av a2 s. c o m*/ public String doInJms(Session session) throws JMSException { TextMessage message = session.createTextMessage(encryptedMessage); Queue outQueue = session.createQueue("receive"); Destination inDest = session.createTemporaryQueue(); String correlationID = UUID.randomUUID().toString(); message.setJMSReplyTo(inDest); message.setJMSCorrelationID(correlationID); MessageProducer producer = session.createProducer(outQueue); producer.send(outQueue, message); return ((TextMessage) session.createConsumer(inDest).receive(10000)).getText(); } }, true); }
From source file:tools.ConsumerTool.java
@Override public void run() { Connection connection = null; Session session = null; try {/* w ww . j av a2 s. c om*/ connection = connectionFactory.createConnection(); if (clientId != null) { connection.setClientID(clientId); } connection.start(); session = connection.createSession(transacted, acknowledgeMode); Destination destination = null; if (jndiLookupDestinations) { destination = (Destination) context.lookup(destinationName); } else { if (useQueueDestinations) { if (useTemporaryDestinations) { destination = session.createTemporaryQueue(); } else { destination = session.createQueue(destinationName); } } else { if (useTemporaryDestinations) { destination = session.createTemporaryTopic(); } else { destination = session.createTopic(destinationName); } } } if (useQueueBrowser) { runQueueBrowser(session, (Queue) destination); } else { MessageConsumer consumer = null; if (useQueueDestinations) { //Queues if (selector != null) { consumer = session.createConsumer(destination, selector); } else { consumer = session.createConsumer(destination); } } else { //Queues if (durable) { //Durable Subscribers if (selector != null) { consumer = session.createDurableSubscriber((Topic) destination, subscriptionName, selector, false); } else { consumer = session.createDurableSubscriber((Topic) destination, subscriptionName); } } else { //Non-Durable Subscribers if (selector != null) { consumer = session.createConsumer(destination, selector); } else { consumer = session.createConsumer(destination); } } } if (useAsyncListener) { final Session consumerSession = session; final AtomicInteger perConsumerReceivedMessages = new AtomicInteger(0); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { perConsumerReceivedMessages.incrementAndGet(); handleMessage(consumerSession, message, perConsumerReceivedMessages.get()); } }); while (perConsumerReceivedMessages.get() < numMessages) { Thread.sleep(100); } } else { int perConsumerReceivedMessages = 0; while (perConsumerReceivedMessages < numMessages) { Message message = null; if (receiveTimeoutMS > -1) { message = consumer.receive(receiveTimeoutMS); } else { message = consumer.receive(); } if (message != null) { perConsumerReceivedMessages++; handleMessage(session, message, perConsumerReceivedMessages); } } } consumer.close(); } } catch (Exception ex) { LOGGER.error("ConsumerTool hit exception: " + ex.getMessage(), ex); } finally { if (session != null) { try { session.close(); } catch (JMSException e) { LOGGER.error("JMSException closing session", e); } } if (connection != null) { try { connection.close(); } catch (JMSException e) { LOGGER.error("JMSException closing connection", e); } } } }
From source file:tools.ProducerTool.java
@Override public void run() { Connection connection = null; Session session = null; try {/*from w ww. ja v a 2s . com*/ connection = connectionFactory.createConnection(); if (clientId != null) { connection.setClientID(clientId); } session = connection.createSession(transacted, acknowledgeMode); Destination destination = null; if (jndiLookupDestinations) { destination = (Destination) context.lookup(destinationName); } else { if (useQueueDestinations) { if (useTemporaryDestinations) { destination = session.createTemporaryQueue(); } else { destination = session.createQueue(destinationName); } } else { if (useTemporaryDestinations) { destination = session.createTemporaryTopic(); } else { destination = session.createTopic(destinationName); } } } MessageProducer producer = session.createProducer(destination); if (durable) { producer.setDeliveryMode(DeliveryMode.PERSISTENT); } else { producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); } int numMessagesToSend = useFinalControlMessage ? numMessages - 1 : numMessages; for (int i = 0; i < numMessagesToSend; i++) { String messageText = "Message " + i + " at " + new Date(); if (bytesLength > -1) { byte[] messageTextBytes = messageText.getBytes(StandardCharsets.UTF_8); BytesMessage bytesMessage = session.createBytesMessage(); bytesMessage.writeBytes(messageTextBytes); if (messageTextBytes.length < bytesLength) { byte[] paddingBytes = new byte[bytesLength - messageTextBytes.length]; bytesMessage.writeBytes(paddingBytes); } if (messageGroupId != null) { bytesMessage.setStringProperty("JMSXGroupID", messageGroupId); } LOGGER.info("Sending bytes message"); producer.send(bytesMessage); } else { TextMessage textMessage = session.createTextMessage(messageText); if (messageGroupId != null) { textMessage.setStringProperty("JMSXGroupID", messageGroupId); } LOGGER.info("Sending text message: " + messageText); producer.send(textMessage); } if (perMessageSleepMS > 0) { Thread.sleep(perMessageSleepMS); } if (transacted) { if ((i + 1) % batchSize == 0) { session.commit(); } } } if (useFinalControlMessage) { Message message = session.createMessage(); if (messageGroupId != null) { message.setStringProperty("JMSXGroupID", messageGroupId); } LOGGER.info("Sending message"); producer.send(message); if (transacted) { session.commit(); } } producer.close(); } catch (Exception ex) { LOGGER.error("ProducerTool hit exception: " + ex.getMessage(), ex); } finally { if (session != null) { try { session.close(); } catch (JMSException e) { LOGGER.error("JMSException closing session", e); } } if (connection != null) { try { connection.close(); } catch (JMSException e) { LOGGER.error("JMSException closing session", e); } } } }
From source file:ubic.gemma.job.grid.util.JMSBrokerMonitorImpl.java
private MapMessage sendTaskSubmissionQueueDiagnosticMessage() throws JMSException { MapMessage reply = jmsTemplate.execute(new SessionCallback<MapMessage>() { @Override/*from www .j a v a 2s. c o m*/ public MapMessage doInJms(Session session) throws JMSException { Queue replyTo = session.createTemporaryQueue(); Message message = session.createMessage(); message.setJMSReplyTo(replyTo); Queue queryQueue = session.createQueue("ActiveMQ.Statistics.Destination.tasks.submit"); MessageProducer producer = session.createProducer(queryQueue); MessageConsumer consumer = session.createConsumer(replyTo); producer.send(message); return (MapMessage) consumer.receive(5000); } }, true); return reply; }