List of usage examples for javax.jms ConnectionFactory createConnection
Connection createConnection() 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 va2 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.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:com.fusesource.examples.activemq.DurableSubscriber.java
public static void main(String args[]) { Connection connection = null; try {/* w w w. j a v a 2 s .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.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.VirtualConsumer.java
public static void main(String args[]) { Connection connection = null; try {// ww 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.setClientID(System.getProperty("clientID")); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(System.getProperty("queue")); MessageConsumer consumer = session.createConsumer(queue); LOG.info("Start consuming messages from " + queue + " 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 w w . j a v a 2s . 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("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:org.dawnsci.commandserver.mx.example.ActiveMQConsumer.java
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = ConnectionFactoryFacade .createConnectionFactory("tcp://sci-serv5.diamond.ac.uk:61616"); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("testQ"); final MessageConsumer consumer = session.createConsumer(queue); connection.start();/*from ww w .ja v a 2 s. c om*/ while (true) { // You have to kill it to stop it! Message m = consumer.receive(1000); if (m != null) { if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; System.out.println(t.getText()); try { ObjectMapper mapper = new ObjectMapper(); final SweepBean colBack = mapper.readValue(t.getText(), SweepBean.class); System.out.println("Data collection found: " + colBack.getDataCollectionId()); } catch (Exception ne) { System.out.println(m + " is not a data collection."); } } else if (m instanceof ObjectMessage) { ObjectMessage o = (ObjectMessage) m; System.out.println(o.getObject()); } } } }
From source file:org.jboss.activemq.clients.JMSConsumer.java
public static void main(String args[]) { Connection connection = null; try {//from w w w .j a v a2 s. c om 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.SimpleProducer.java
public static void main(String args[]) { Connection connection = null; try {/*from www. ja v a 2s . com*/ // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); Destination destination = (Destination) context.lookup(DESTINATION_NAME); connection = factory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); 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.toString() + " 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:com.fusesource.examples.activemq.SimpleConsumer.java
public static void main(String args[]) { Connection connection = null; try {/*from ww w .j a v a2 s. co m*/ // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); Destination destination = (Destination) context.lookup(DESTINATION_NAME); connection = factory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(destination); LOG.info("Start consuming messages from " + destination.toString() + " 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:org.apache.activemq.demo.SimpleConsumer.java
/** * @param args the queue used by the example *///from w w w .j a v a 2s .c o m public static void main(String[] args) { String destinationName = null; Context jndiContext = null; ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Destination destination = null; MessageConsumer consumer = null; /* * Read destination name from command line and display it. */ if (args.length != 1) { LOG.info("Usage: java SimpleConsumer <destination-name>"); System.exit(1); } destinationName = args[0]; LOG.info("Destination name is " + destinationName); /* * Create a JNDI API InitialContext object */ try { jndiContext = new InitialContext(); } catch (NamingException e) { LOG.info("Could not create JNDI API " + "context: " + e.toString()); System.exit(1); } /* * Look up connection factory and destination. */ try { connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); destination = (Destination) jndiContext.lookup(destinationName); } catch (NamingException e) { LOG.info("JNDI API lookup failed: " + e.toString()); System.exit(1); } /* * Create connection. Create session from connection; false means * session is not transacted. Create receiver, then start message * delivery. Receive all text messages from destination until a non-text * message is received indicating end of message stream. Close * connection. */ try { connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumer = session.createConsumer(destination); connection.start(); while (true) { Message m = consumer.receive(1); if (m != null) { if (m instanceof TextMessage) { TextMessage message = (TextMessage) m; LOG.info("Reading message: " + message.getText()); } else { break; } } } } catch (JMSException e) { LOG.info("Exception occurred: " + e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } }
From source file:io.datalayer.activemq.consumer.SimpleConsumer.java
/** * @param args the queue used by the example *//*w ww . j ava2 s . co m*/ public static void main(String... args) { String destinationName = null; Context jndiContext = null; ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Destination destination = null; MessageConsumer consumer = null; /* * Read destination name from command line and display it. */ if (args.length != 1) { LOG.info("Usage: java SimpleConsumer <destination-name>"); System.exit(1); } destinationName = args[0]; LOG.info("Destination name is " + destinationName); /* * Create a JNDI API InitialContext object */ try { jndiContext = new InitialContext(); } catch (NamingException e) { LOG.info("Could not create JNDI API " + "context: " + e.toString()); System.exit(1); } /* * Look up connection factory and destination. */ try { connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); destination = (Destination) jndiContext.lookup(destinationName); } catch (NamingException e) { LOG.info("JNDI API lookup failed: " + e.toString()); System.exit(1); } /* * Create connection. Create session from connection; false means * session is not transacted. Create receiver, then start message * delivery. Receive all text messages from destination until a non-text * message is received indicating end of message stream. Close * connection. */ try { connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumer = session.createConsumer(destination); connection.start(); while (true) { Message m = consumer.receive(1); if (m != null) { if (m instanceof TextMessage) { TextMessage message = (TextMessage) m; LOG.info("Reading message: " + message.getText()); } else { break; } } } } catch (JMSException e) { LOG.info("Exception occurred: " + e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } }