List of usage examples for javax.jms ConnectionFactory createConnection
Connection createConnection() throws JMSException;
From source file:io.datalayer.activemq.producer.SimpleProducer.java
/** * @param args the destination name to send to and optionally, the number of * messages to send/* ww w . j av a 2 s .c o m*/ */ public static void main(String... args) { Context jndiContext = null; ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Destination destination = null; MessageProducer producer = null; String destinationName = null; final int numMsgs; if ((args.length < 1) || (args.length > 2)) { LOG.info("Usage: java SimpleProducer <destination-name> [<number-of-messages>]"); System.exit(1); } destinationName = args[0]; LOG.info("Destination name is " + destinationName); if (args.length == 2) { numMsgs = (new Integer(args[1])).intValue(); } else { numMsgs = 1; } /* * 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); System.exit(1); } /* * Create connection. Create session from connection; false means * session is not transacted. Create sender and text message. Send * messages, varying text slightly. Send end-of-messages message. * Finally, close connection. */ try { connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); producer = session.createProducer(destination); TextMessage message = session.createTextMessage(); for (int i = 0; i < numMsgs; i++) { message.setText("This is message " + (i + 1)); LOG.info("Sending message: " + message.getText()); producer.send(message); } /* * Send a non-text control message indicating end of messages. */ producer.send(session.createMessage()); } catch (JMSException e) { LOG.info("Exception occurred: " + e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } }
From source file:org.dawnsci.commandserver.core.util.JSONUtils.java
public static final void sendTopic(Object message, String topicName, URI uri) throws Exception { Connection connection = null; try {//from w w w . j a v a 2s .c om ConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); connection = connectionFactory.createConnection(); connection.start(); sendTopic(connection, message, topicName, uri); } finally { if (connection != null) connection.close(); } }
From source file:service.emailer.Emailer.java
/** * Send email with parameters to emailQueue * @param area/*from www .j ava 2 s.c o m*/ * @param email */ public static void sendEmail(String area, List<String> email) { try { //JMS QUEUE SEND final ConnectionFactory connectionFactory = lookup(ConnectionFactory.class, JNDI_CONNECTION_FACTORY); final Queue queue = lookup(Queue.class, JNDI_QUEUE); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); MapMessage message = session.createMapMessage(); message.setString("type", "warnFile"); message.setString("area", area); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(email); message.setString("emailReceivers", json); producer.send(message); } catch (JMSException ex) { Logger.getLogger(Emailer.class.getName()).log(Level.SEVERE, null, ex); } catch (JsonProcessingException ex) { Logger.getLogger(Emailer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.apache.falcon.messaging.JMSMessageConsumer.java
private static Connection createAndGetConnection(String implementation, String userName, String password, String url) throws JMSException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { @SuppressWarnings("unchecked") Class<ConnectionFactory> clazz = (Class<ConnectionFactory>) JMSMessageConsumer.class.getClassLoader() .loadClass(implementation);// w w w . ja v a 2 s .c o m ConnectionFactory connectionFactory = clazz.getConstructor(String.class, String.class, String.class) .newInstance(userName, password, url); return connectionFactory.createConnection(); }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static Connection getQueueConnection() throws InfrastructureException { InitialContext jndiContext;//www . ja v a2s . c o m Connection connection; try { jndiContext = JNDIUtil.getInitialContext(); ConnectionFactory qcf = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); connection = qcf.createConnection(); } catch (Exception e) { log.error("unsable to get queue connnetion", e); throw new InfrastructureException("unsable to get queue connnetion"); } return connection; }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static Connection registerListenerOnQueue(MessageListener listener, String queueName) throws Exception { InitialContext jndiContext = JNDIUtil.getInitialContext(); Queue queue = (Queue) jndiContext.lookup("queue/" + queueName); ConnectionFactory qcf = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); Connection connection = qcf.createConnection(); Session m_session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer m_receiver = m_session.createConsumer(queue); m_receiver.setMessageListener(listener); return connection; }
From source file:org.wso2.carbon.bpmn.extensions.jms.JMSUtils.java
/** * * @param connectionFactory/*from ww w . ja v a 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.firstopen.singularity.util.JMSUtil.java
public static void deliverMessageToTopic(String host, String topicName, String xml) { log.debug("IntegrationMod.deliverMessageToQueue queueName = " + topicName + " and doc = " + xml); char test = topicName.charAt(0); if (test == '/') topicName = topicName.substring(1); try {//from ww w .j ava2 s. c o m InitialContext context = JNDIUtil.getInitialContext(host); Connection connection = null; Session session = null; MessageProducer publisher = null; ConnectionFactory tcf = (ConnectionFactory) context.lookup("ConnectionFactory"); Topic topic = (Topic) context.lookup(topicName); connection = tcf.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); publisher = session.createProducer(topic); TextMessage message = session.createTextMessage(); log.debug("message value is -> " + xml); message.setText(xml); publisher.send(message); } catch (Exception e) { log.error("unable to send message on topic", e); } finally { } }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static void deliverMessageToQueue(String host, String queueName, String xml) { log.debug("IntegrationMod.deliverMessageToQueue queueName = " + queueName + " and doc = " + xml); MessageProducer m_sender = null;/* w ww .j a v a 2s .c om*/ Session m_session = null; Connection connection = null; char test = queueName.charAt(0); if (test == '/') queueName = queueName.substring(1); try { InitialContext context = JNDIUtil.getInitialContext(host); ConnectionFactory qcf = (ConnectionFactory) context.lookup("ConnectionFactory"); Queue queue = (Queue) context.lookup("queue/" + queueName); connection = qcf.createConnection(); m_session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); m_sender = m_session.createProducer(queue); TextMessage message = m_session.createTextMessage(); log.debug("message value is -> " + xml); message.setText(xml); m_sender.send(message); } catch (Exception e) { log.error("IntegrationMod.deliverMessageToQueue() Exception = ", e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { log.error("unable ot close JMS Connection", e); } } } }
From source file:org.fao.geonet.jms.ClusterConfig.java
private static void initJMSConnection() throws ClusterException { try {/* ww w .j av a 2s . c o m*/ // Getting JMS connection from the server System.out.println("Getting JMS Connection"); ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ClusterConfig.getBrokerURL()); connection = connectionFactory.createConnection(); connection.setClientID(ClusterConfig.getClientID()); connection.start(); // Creating session for sending and receiving messages session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Set connection and session for all JMSActors JMSActor.connection = connection; JMSActor.session = session; System.out.println("Getiing JMS Connection successfull"); } catch (JMSException x) { System.out.println("Getiing JMS Connection failed"); System.err.println(x.getMessage()); x.printStackTrace(); throw new ClusterException(x.getMessage(), x); } }