List of usage examples for javax.jms Session createTopic
Topic createTopic(String topicName) throws JMSException;
From source file:com.fusesource.examples.activemq.SimplePublisher.java
public static void main(String args[]) { Connection connection = null; try {//from w w w .j a va 2s . c om // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); connection = factory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(DESTINATION_NAME); MessageProducer producer = session.createProducer(topic); producer.setTimeToLive(MESSAGE_TIME_TO_LIVE_MILLISECONDS); for (int i = 1; i <= NUM_MESSAGES_TO_BE_SENT; i++) { TextMessage message = session.createTextMessage(i + ". message sent"); LOG.info("Sending to destination: " + DESTINATION_NAME + " this text: '" + message.getText()); producer.send(message); Thread.sleep(MESSAGE_DELAY_MILLISECONDS); } // Cleanup producer.close(); session.close(); } catch (Throwable t) { LOG.error(t); } finally { // Cleanup code // In general, you should always close producers, consumers, // sessions, and connections in reverse order of creation. // For this simple example, a JMS connection.close will // clean up all other resources. if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error(e); } } } }
From source file:example.topic.durable.Publisher.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();//from w w w . j a v a 2 s .c o m } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic("test-topic"); MessageProducer producer = session.createProducer(destination); for (int i = 0; i < NUM_MESSAGES_TO_SEND; i++) { TextMessage message = session.createTextMessage("Message #" + i); System.out.println("Sending message #" + i); producer.send(message); Thread.sleep(DELAY); } // tell the subscribers we're done producer.send(session.createTextMessage("END")); producer.close(); session.close(); } catch (Exception e) { System.out.println("Caught exception!"); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println("Could not close an open connection..."); } } } }
From source file:example.topic.durable.Subscriber.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();/* w ww.ja va 2 s . c o m*/ } System.out .println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; final CountDownLatch latch = new CountDownLatch(1); try { connection = connectionFactory.createConnection(); String clientId = System.getProperty("clientId"); connection.setClientID(clientId); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Topic destination = session.createTopic("test-topic"); MessageConsumer consumer = session.createDurableSubscriber(destination, clientId); consumer.setMessageListener(new Subscriber(latch)); latch.await(); consumer.close(); session.close(); } catch (Exception e) { System.out.println("Caught exception!"); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println("Could not close an open connection..."); } } } }
From source file:example.topic.Subscriber.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();/*from www . j a v a 2 s .co m*/ } System.out .println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; final CountDownLatch latch = new CountDownLatch(1); try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic("test-topic"); MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new Subscriber(latch)); latch.await(); consumer.close(); session.close(); } catch (Exception e) { System.out.println("Caught exception!"); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println("Could not close an open connection..."); } } } }
From source file:org.jboss.activemq.clients.JMSConsumer.java
public static void main(String args[]) { Connection connection = null; try {/*from w ww . j av a 2s .c o m*/ Options options = new Options(); options.addOption("h", "help", false, "help:"); options.addOption("url", true, "url for the broker to connect to"); options.addOption("u", "username", true, "User name for connection"); options.addOption("p", "password", true, "password for connection"); options.addOption("d", "destination", true, "destination to send to"); options.addOption("n", "number", true, "number of messages to send"); options.addOption("delay", true, "delay between each send"); CommandLineParser parser = new BasicParser(); CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption("h")) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp("OptionsTip", options); System.exit(0); } String url = commandLine.hasOption("host") ? commandLine.getOptionValue("host") : URL; String userName = commandLine.hasOption("u") ? commandLine.getOptionValue("u") : "admin"; String password = commandLine.hasOption("p") ? commandLine.getOptionValue("p") : "admin"; String destinationName = commandLine.hasOption("d") ? commandLine.getOptionValue("d") : DESTINATION_NAME; int numberOfMessages = commandLine.hasOption("n") ? Integer.parseInt(commandLine.getOptionValue("n")) : NUM_MESSAGES_TO_RECEIVE; ; ConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, url); connection = factory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(destinationName); MessageConsumer consumer = session.createConsumer(topic); LOG.info("Start consuming " + numberOfMessages + " messages from " + topic.toString()); for (int i = 0; i < numberOfMessages; i++) { Message message = consumer.receive(); if (message != null) { if (message instanceof BytesMessage) { BytesMessage bytesMessage = (BytesMessage) message; int len = (int) bytesMessage.getBodyLength(); byte[] data = new byte[len]; bytesMessage.readBytes(data); String value = new String(data); LOG.info("Got " + value); } else { LOG.info("Got a message " + message); } } } consumer.close(); session.close(); } catch (Throwable t) { LOG.error("Error receiving message", t); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error("Error closing connection", e); } } } }
From source file:com.fusesource.examples.activemq.DurableSubscriber.java
public static void main(String args[]) { Connection connection = null; try {/*from www . j a v a 2 s. c o m*/ // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); connection = factory.createConnection(); connection.setClientID(System.getProperty("clientID")); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.CLIENT_ACKNOWLEDGE); String topicName = System.getProperty("topic"); Topic topic = session.createTopic(topicName); MessageConsumer consumer = session.createDurableSubscriber(topic, "Test_Durable_Subscriber"); LOG.info("Start consuming messages from " + topicName + " with " + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout"); // Synchronous message consumer int i = 1; while (true) { Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); LOG.info("Got " + (i++) + ". message: " + text); } } else { break; } } consumer.close(); session.close(); } catch (Throwable t) { LOG.error(t); } finally { // Cleanup code // In general, you should always close producers, consumers, // sessions, and connections in reverse order of creation. // For this simple example, a JMS connection.close will // clean up all other resources. if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error(e); } } } }
From source file:com.fusesource.examples.activemq.SimpleSubscriber.java
public static void main(String args[]) { Connection connection = null; try {//from w ww .j a va 2 s . co m // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); connection = factory.createConnection(); connection.setClientID("ApplicationA"); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); String topicName = System.getProperty("topic"); Topic topic = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(topic); LOG.info("Start consuming messages from " + topicName + " with " + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout"); // Synchronous message consumer int i = 1; while (true) { Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); LOG.info("Got " + (i++) + ". message: " + text); } } else { break; } } consumer.close(); session.close(); } catch (Throwable t) { LOG.error(t); } finally { // Cleanup code // In general, you should always close producers, consumers, // sessions, and connections in reverse order of creation. // For this simple example, a JMS connection.close will // clean up all other resources. if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error(e); } } } }
From source file:example.wildcard.Client.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();/*from w ww . j av a 2 s. co m*/ } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; try { Topic senderTopic = new ActiveMQTopic(System.getProperty("topicName")); connection = connectionFactory.createConnection("admin", "password"); Session senderSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageProducer sender = senderSession.createProducer(senderTopic); Session receiverSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); String policyType = System.getProperty("wildcard", ".*"); String receiverTopicName = senderTopic.getTopicName() + policyType; Topic receiverTopic = receiverSession.createTopic(receiverTopicName); MessageConsumer receiver = receiverSession.createConsumer(receiverTopic); receiver.setMessageListener(new MessageListener() { public void onMessage(Message message) { try { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("We received a new message: " + text); } } catch (JMSException e) { System.out.println("Could not read the receiver's topic because of a JMSException"); } } }); connection.start(); System.out.println("Listening on '" + receiverTopicName + "'"); System.out.println("Enter a message to send: "); Scanner inputReader = new Scanner(System.in); while (true) { String line = inputReader.nextLine(); if (line == null) { System.out.println("Done!"); break; } else if (line.length() > 0) { try { TextMessage message = senderSession.createTextMessage(); message.setText(line); System.out.println("Sending a message: " + message.getText()); sender.send(message); } catch (JMSException e) { System.out.println("Exception during publishing a message: "); } } } receiver.close(); receiverSession.close(); sender.close(); senderSession.close(); } catch (Exception e) { System.out.println("Caught exception!"); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println("When trying to close connection: "); } } } }
From source file:example.composite.dest.Consumer.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();// ww w . j a v a 2 s . c om } System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("test-queue"); Destination destinationFoo = session.createQueue("test-queue-foo"); Destination destinationBar = session.createQueue("test-queue-bar"); Destination destinationTopicFoo = session.createTopic("test-topic-foo"); MessageConsumer consumer = session.createConsumer(destination); MessageConsumer consumerFoo = session.createConsumer(destinationFoo); MessageConsumer consumerBar = session.createConsumer(destinationBar); MessageConsumer consumerTopicFoo = session.createConsumer(destinationTopicFoo); int i = 0; while (true) { Message message = consumer.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-queue: " + text); } } else { break; } message = consumerFoo.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-queue-foo: " + text); } } else { break; } message = consumerBar.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-queue-bar: " + text); } } else { break; } message = consumerTopicFoo.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-topic-bar: " + text); } } else { break; } } consumer.close(); session.close(); } catch (Exception e) { System.out.println("Caught exception!"); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println("Could not close an open connection..."); } } } }
From source file:org.dawnsci.commandserver.core.util.JSONUtils.java
/** * Generic way of sending a topic notification * @param connection - does not get closed afterwards nust be started before. * @param message/*from w w w . j a va 2s . c o m*/ * @param topicName * @param uri * @throws Exception */ private static final void sendTopic(Connection connection, Object message, String topicName, URI uri) throws Exception { // JMS messages are sent and received using a Session. We will // create here a non-transactional session object. If you want // to use transactions you should set the first parameter to 'true' Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); try { Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); final ObjectMapper mapper = new ObjectMapper(); // Here we are sending the message out to the topic TextMessage temp = session.createTextMessage(mapper.writeValueAsString(message)); producer.send(temp, DeliveryMode.NON_PERSISTENT, 1, 5000); } finally { session.close(); } }