Example usage for javax.jms TopicConnectionFactory createTopicConnection

List of usage examples for javax.jms TopicConnectionFactory createTopicConnection

Introduction

In this page you can find the example usage for javax.jms TopicConnectionFactory createTopicConnection.

Prototype


TopicConnection createTopicConnection() throws JMSException;

Source Link

Document

Creates a topic connection with the default user identity.

Usage

From source file:eu.learnpad.simulator.mon.manager.ResponseDispatcher.java

public ResponseDispatcher(InitialContext initConn, TopicConnectionFactory connectionFact,
        HashMap<Object, ConsumerProfile> requestMap, LearnerAssessmentManager learnerAssessmentManager) {

    ResponseDispatcher.requestMap = requestMap;
    ResponseDispatcher.initConn = initConn;
    ResponseDispatcher.lam = learnerAssessmentManager;
    try {//from   www.  j  a va 2  s .  c o m
        connection = connectionFact.createTopicConnection();
        publishSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    } catch (JMSException e) {
        e.printStackTrace();
    }
}

From source file:eu.learnpad.simulator.mon.probe.GlimpseAbstractProbe.java

/**
 * This method setup a {@link TopicConnection} object.
 * /*from  w  ww.ja  v a  2  s  .  c o  m*/
 * @param initConn the InitialContext object generated using the method {@link #initConnection(Properties, boolean)}.
 * @param settings can be generated automatically using {@link Manager#createProbeSettingsPropertiesObject(String, String, String, String, String, String, boolean, String, String)}
 * @param probeChannel
 * @param settings
 * @param debug
 * @return a {@link TopicPublisher} object
 * @throws NamingException
 * @throws JMSException
 */
protected TopicPublisher createConnection(InitialContext initConn, String probeChannel, Properties settings,
        boolean debug) throws NamingException, JMSException {
    if (debug)
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating ConnectionFactory with settings ");
    TopicConnectionFactory connFact = (TopicConnectionFactory) initConn
            .lookup(settings.getProperty("connectionFactoryNames"));
    if (debug) {
        DebugMessages.ok();
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating TopicConnection ");
    }
    connection = connFact.createTopicConnection();
    if (debug) {
        DebugMessages.ok();
        DebugMessages.line();
    }
    if (debug) {
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Creating Session ");
    }
    publishSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    if (debug) {
        DebugMessages.ok();
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Looking up for channel ");
    }
    connectionTopic = (Topic) initContext.lookup(probeChannel);
    if (debug) {
        DebugMessages.ok();
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Creating Publisher ");
    }
    return tPub = publishSession.createPublisher(connectionTopic);
}

From source file:org.grouter.common.jms.TopicListenerDestination.java

/**
 * Connect to topic  and open a session.
 *//*from  w  w  w.j  a va2s  . c  o m*/
@Override
public void bind() {
    try {
        // Find ConnectionFactory
        final TopicConnectionFactory topicConnectionFactory = getInstance()
                .getTopicConnectionFactory(connectionFactory, context);
        // Get queue
        topic = getInstance().getTopic(destinationName, context);
        // Create conneciton to queue
        topicConnection = topicConnectionFactory.createTopicConnection();
        // Register an exceptionlistener
        topicConnection.setExceptionListener(exceptionListener);
        topicSession = topicConnection.createTopicSession(isTransactional, acknowledgeMode);

        messageConsumer = topicSession.createConsumer(topic);

        // Sets the receiver which onMessage method will be called.
        messageConsumer.setMessageListener(listener);

        topicConnection.start();
        logger.info("Bound to destination " + destinationName);
    } catch (JMSException e) {
        logger.error("Got exception with JMS provider during bind to destination " + destinationName
                + ". Error code : " + e.getErrorCode(), e);
        rebind(this);
    } catch (ServiceLocatorException ex) {
        logger.error("Got exception with JMS provider during bind to destination " + destinationName + ".", ex);
        rebind(this);
    }
}

From source file:org.grouter.common.jms.TopicSenderDestination.java

@Override
public void bind() {
    try {/*from  w w  w  .j  av  a 2s . c  o m*/
        // Find ConnectionFactory
        final TopicConnectionFactory topicConnectionFactory = getInstance()
                .getTopicConnectionFactory(connectionFactory, context);
        // Get queue
        final Topic topic = getInstance().getTopic(destinationName, context);
        // Create conneciton to queue
        topicConnection = topicConnectionFactory.createTopicConnection();
        // Register an exceptionlistener
        topicConnection.setExceptionListener(exceptionListener);
        topicSession = topicConnection.createTopicSession(isTransactional, acknowledgeMode);
        topicPublisher = topicSession.createPublisher(topic);
        if (timeToLive > 0) {
            topicPublisher.setTimeToLive(timeToLive);
        }
        if (useTemporaryReplyDestination) {
            temporaryTopic = topicSession.createTemporaryTopic();
            logger.debug("TemporaryTopic created for this session " + temporaryTopic);
        }
        topicConnection.start();
        logger.info("Bound to destination " + destinationName);
    } catch (JMSException e) {
        logger.error("Got exception with JMS provider during bind to destination " + destinationName
                + ". Error code : " + e.getErrorCode(), e);
        rebind(this);
    } catch (ServiceLocatorException ex) {
        logger.error("Got exception with JMS provider during bind to destination " + destinationName + ".", ex);
        rebind(this);
    }
}

From source file:org.sdnmq.jms.PacketHandler.java

/**
 * Initialization of JMS./*  w  w  w  . j a  v  a  2  s  .co m*/
 */
private void initMQ() {
    log.trace("Setting up JMS ...");

    Properties jndiProps = JNDIHelper.getJNDIProperties();

    Context ctx = null;
    try {
        ctx = new InitialContext(jndiProps);
    } catch (NamingException e) {
        log.error(e.getMessage());
        releaseMQ();
        return;
    }

    TopicConnectionFactory topicFactory = null;
    try {
        topicFactory = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
    } catch (NamingException e) {
        log.error(e.getMessage());
        releaseMQ();
        return;
    }

    try {
        connection = topicFactory.createTopicConnection();
    } catch (JMSException e) {
        log.error("Could not create JMS connection: " + e.getMessage());
        releaseMQ();
        return;
    }

    try {
        session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    } catch (JMSException e) {
        log.error("Could not create JMS session: " + e.getMessage());
        releaseMQ();
        return;
    }

    // Get the JNDI object name of the packet-in topic object from the OpenDaylight configuration.
    String topicName = System.getProperty(PACKETIN_TOPIC_PROPERTY, DEFAULT_PACKETIN_TOPIC_NAME);
    log.info("Using the following topic for packet-in events: " + topicName);
    try {
        packetinTopic = (Topic) ctx.lookup(topicName);
    } catch (NamingException e) {
        log.error("Could not resolve topic object: " + e.getMessage());
        releaseMQ();
        return;
    }

    try {
        publisher = session.createPublisher(packetinTopic);
    } catch (JMSException e) {
        log.error(e.getMessage());
        releaseMQ();
        return;
    }

    log.trace("JMS setup finished successfully");
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.factory.JMSConnectionFactory.java

public Connection createConnection() {
    if (connectionFactory == null) {
        logger.error("Connection cannot be establish to the broker. Please check the broker libs provided.");
        return null;
    }/*from www  .j av  a 2 s  .  co  m*/
    Connection connection = null;
    try {
        if ("1.1".equals(jmsSpec)) {
            if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) {
                connection = ((QueueConnectionFactory) (this.connectionFactory)).createQueueConnection();
            } else if (this.destinationType.equals(JMSConstants.JMSDestinationType.TOPIC)) {
                connection = ((TopicConnectionFactory) (this.connectionFactory)).createTopicConnection();
            }
            if (isDurable) {
                connection.setClientID(clientId);
            }
            return connection;
        } else {
            QueueConnectionFactory qConFac = null;
            TopicConnectionFactory tConFac = null;
            if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) {
                qConFac = (QueueConnectionFactory) this.connectionFactory;
            } else {
                tConFac = (TopicConnectionFactory) this.connectionFactory;
            }
            if (qConFac != null) {
                connection = qConFac.createQueueConnection();
            } else if (tConFac != null) {
                connection = tConFac.createTopicConnection();
            }
            if (isDurable && !isSharedSubscription) {
                connection.setClientID(clientId);
            }
            return connection;
        }
    } catch (JMSException e) {
        logger.error("JMS Exception while creating connection through factory '" + this.connectionFactoryString
                + "' " + e.getMessage(), e);
        // Need to close the connection in the case if durable subscriptions
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ex) {
            }
        }
    }

    return null;
}

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.j av  a 2  s. c  o  m*/
    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  ww .j a v  a  2s.co m
    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);
    }
}