Example usage for javax.jms Session createTopic

List of usage examples for javax.jms Session createTopic

Introduction

In this page you can find the example usage for javax.jms Session createTopic.

Prototype

Topic createTopic(String topicName) throws JMSException;

Source Link

Document

Creates a Topic object which encapsulates a specified provider-specific topic name.

Usage

From source file:org.seedstack.jms.internal.JmsPlugin.java

private void configureMessageListeners(Collection<Class<?>> listenerCandidates) {
    for (Class<?> candidate : listenerCandidates) {
        if (MessageListener.class.isAssignableFrom(candidate)) {
            //noinspection unchecked
            Class<? extends MessageListener> messageListenerClass = (Class<? extends MessageListener>) candidate;
            String messageListenerName = messageListenerClass.getCanonicalName();
            JmsMessageListener annotation = messageListenerClass.getAnnotation(JmsMessageListener.class);

            boolean isTransactional;
            try {
                isTransactional = transactionPlugin
                        .isTransactional(messageListenerClass.getMethod("onMessage", Message.class));
            } catch (NoSuchMethodException e) {
                throw SeedException.wrap(e, JmsErrorCodes.UNEXPECTED_EXCEPTION);
            }/*w  w w  .  j a  va2 s  .  c  om*/

            Connection listenerConnection = connections.get(annotation.connection());

            if (listenerConnection == null) {
                throw SeedException.createNew(JmsErrorCodes.MISSING_CONNECTION_FACTORY)
                        .put(ERROR_CONNECTION_NAME, annotation.connection())
                        .put(ERROR_MESSAGE_LISTENER_NAME, messageListenerName);
            }

            Session session;
            try {
                session = listenerConnection.createSession(isTransactional, Session.AUTO_ACKNOWLEDGE);
            } catch (JMSException e) {
                throw SeedException.wrap(e, JmsErrorCodes.UNABLE_TO_CREATE_SESSION)
                        .put(ERROR_CONNECTION_NAME, annotation.connection())
                        .put(ERROR_MESSAGE_LISTENER_NAME, messageListenerName);
            }

            Destination destination;
            DestinationType destinationType;

            if (!annotation.destinationTypeStr().isEmpty()) {
                try {
                    destinationType = DestinationType
                            .valueOf(application.substituteWithConfiguration(annotation.destinationTypeStr()));
                } catch (IllegalArgumentException e) {
                    throw SeedException.wrap(e, JmsErrorCodes.UNKNOWN_DESTINATION_TYPE)
                            .put(ERROR_DESTINATION_TYPE, annotation.destinationTypeStr())
                            .put(ERROR_CONNECTION_NAME, annotation.connection())
                            .put(ERROR_MESSAGE_LISTENER_NAME, messageListenerName);
                }
            } else {
                destinationType = annotation.destinationType();
            }
            try {
                switch (destinationType) {
                case QUEUE:
                    destination = session
                            .createQueue(application.substituteWithConfiguration(annotation.destinationName()));
                    break;
                case TOPIC:
                    destination = session
                            .createTopic(application.substituteWithConfiguration(annotation.destinationName()));
                    break;
                default:
                    throw SeedException.createNew(JmsErrorCodes.UNKNOWN_DESTINATION_TYPE)
                            .put(ERROR_CONNECTION_NAME, annotation.connection())
                            .put(ERROR_MESSAGE_LISTENER_NAME, messageListenerName);
                }
            } catch (JMSException e) {
                throw SeedException.wrap(e, JmsErrorCodes.UNABLE_TO_CREATE_DESTINATION)
                        .put(ERROR_DESTINATION_TYPE, destinationType.name())
                        .put(ERROR_CONNECTION_NAME, annotation.connection())
                        .put(ERROR_MESSAGE_LISTENER_NAME, messageListenerName);
            }

            Class<? extends MessagePoller> messagePollerClass = null;
            if (annotation.poller().length > 0) {
                messagePollerClass = annotation.poller()[0];
            }

            registerMessageListener(new MessageListenerDefinition(messageListenerName,
                    application.substituteWithConfiguration(annotation.connection()), session, destination,
                    application.substituteWithConfiguration(annotation.selector()), messageListenerClass,
                    messagePollerClass));
        }
    }
}

From source file:org.wso2.carbon.apimgt.jms.listener.utils.JMSTaskManager.java

/**
 * Return the JMS Destination for the JNDI name of the Destination from the InitialContext
 *
 * @param session which is used to create the destinations if not present and if possible
 * @return the JMS Destination to which this STM listens for messages
 *///  ww w. j  a  v  a 2  s. co  m
private Destination getDestination(Session session) {
    if (destination == null) {
        try {
            context = getInitialContext();
            destination = JMSUtils.lookupDestination(context, getDestinationJNDIName(),
                    JMSUtils.getDestinationTypeAsString(destinationType));
            if (log.isDebugEnabled()) {
                log.debug("JMS Destination with JNDI name : " + getDestinationJNDIName() + " found "
                        + jmsConsumerName);
            }
        } catch (NamingException e) {
            try {
                switch (destinationType) {
                case JMSConstants.QUEUE: {
                    destination = session.createQueue(getDestinationJNDIName());
                    break;
                }
                case JMSConstants.TOPIC: {
                    destination = session.createTopic("BURL:" + getDestinationJNDIName());
                    break;
                }
                default: {
                    handleException("Error looking up JMS destination : " + getDestinationJNDIName()
                            + " using JNDI properties : " + jmsProperties, e);
                }
                }
            } catch (JMSException j) {
                handleException("Error looking up JMS destination and auto " + "creating JMS destination : "
                        + getDestinationJNDIName() + " using JNDI properties : " + jmsProperties, e);
            }
        }
    }
    return destination;
}

From source file:org.wso2.carbon.event.input.adapter.jms.internal.util.JMSTaskManager.java

/**
 * Return the JMS Destination for the JNDI name of the Destination from the InitialContext
 *
 * @param session which is used to create the destinations if not present and if possible
 * @return the JMS Destination to which this STM listens for messages
 *//*from  www.  ja  v a  2  s  . c o  m*/
private Destination getDestination(Session session) {
    if (destination == null) {
        try {
            context = getInitialContext();
            destination = JMSUtils.lookupDestination(context, getDestinationJNDIName(),
                    JMSUtils.getDestinationTypeAsString(destinationType));
            if (log.isDebugEnabled()) {
                log.debug("JMS Destination with JNDI name : " + getDestinationJNDIName()
                        + " found for event adapter " + eventAdapterName);
            }
        } catch (NamingException e) {
            try {
                switch (destinationType) {
                case JMSConstants.QUEUE: {
                    destination = session.createQueue(getDestinationJNDIName());
                    break;
                }
                case JMSConstants.TOPIC: {
                    destination = session.createTopic(getDestinationJNDIName());
                    break;
                }
                default: {
                    handleException("Error looking up JMS destination : " + getDestinationJNDIName()
                            + " using JNDI properties : " + jmsProperties, e);
                }
                }
            } catch (JMSException j) {
                handleException("Error looking up JMS destination and auto " + "creating JMS destination : "
                        + getDestinationJNDIName() + " using JNDI properties : " + jmsProperties, e);
            }
        }
    }
    return destination;
}

From source file:org.wso2.carbon.event.input.adaptor.jms.internal.util.JMSTaskManager.java

/**
 * Return the JMS Destination for the JNDI name of the Destination from the InitialContext
 *
 * @param session which is used to create the destinations if not present and if possible
 * @return the JMS Destination to which this STM listens for messages
 *//*w  ww .j  a va  2s . c  om*/
private Destination getDestination(Session session) {
    if (destination == null) {
        try {
            context = getInitialContext();
            destination = JMSUtils.lookupDestination(context, getDestinationJNDIName(),
                    JMSUtils.getDestinationTypeAsString(destinationType));
            if (log.isDebugEnabled()) {
                log.debug("JMS Destination with JNDI name : " + getDestinationJNDIName()
                        + " found for event adaptor " + eventAdaptorName);
            }
        } catch (NamingException e) {
            try {
                switch (destinationType) {
                case JMSConstants.QUEUE: {
                    destination = session.createQueue(getDestinationJNDIName());
                    break;
                }
                case JMSConstants.TOPIC: {
                    destination = session.createTopic(getDestinationJNDIName());
                    break;
                }
                default: {
                    handleException("Error looking up JMS destination : " + getDestinationJNDIName()
                            + " using JNDI properties : " + jmsProperties, e);
                }
                }
            } catch (JMSException j) {
                handleException("Error looking up JMS destination and auto " + "creating JMS destination : "
                        + getDestinationJNDIName() + " using JNDI properties : " + jmsProperties, e);
            }
        }
    }
    return destination;
}

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 w w .j  a va  2s .  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   ww  w .  j  av  a2  s.c o  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);
    }
}

From source file:org.apache.camel.component.jms.JmsProducer.java

protected boolean processInOnly(final Exchange exchange, final AsyncCallback callback) {
    final org.apache.camel.Message in = exchange.getIn();

    String destinationName = in.getHeader(JmsConstants.JMS_DESTINATION_NAME, String.class);
    if (destinationName != null) {
        // remove the header so it wont be propagated
        in.removeHeader(JmsConstants.JMS_DESTINATION_NAME);
    }/*from   www . j a va2s .  co m*/
    if (destinationName == null) {
        destinationName = endpoint.getDestinationName();
    }

    Destination destination = in.getHeader(JmsConstants.JMS_DESTINATION, Destination.class);
    if (destination != null) {
        // remove the header so it wont be propagated
        in.removeHeader(JmsConstants.JMS_DESTINATION);
    }
    if (destination == null) {
        destination = endpoint.getDestination();
    }
    if (destination != null) {
        // prefer to use destination over destination name
        destinationName = null;
    }
    final String to = destinationName != null ? destinationName : "" + destination;

    MessageCreator messageCreator = new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
            Message answer = endpoint.getBinding().makeJmsMessage(exchange, in, session, null);

            // when in InOnly mode the JMSReplyTo is a bit complicated
            // we only want to set the JMSReplyTo on the answer if
            // there is a JMSReplyTo from the header/endpoint and
            // we have been told to preserveMessageQos

            Object jmsReplyTo = answer.getJMSReplyTo();
            if (endpoint.isDisableReplyTo()) {
                // honor disable reply to configuration
                if (LOG.isDebugEnabled()) {
                    LOG.debug("ReplyTo is disabled on endpoint: " + endpoint);
                }
                answer.setJMSReplyTo(null);
            } else {
                // if the binding did not create the reply to then we have to try to create it here
                if (jmsReplyTo == null) {
                    // prefer reply to from header over endpoint configured
                    jmsReplyTo = exchange.getIn().getHeader("JMSReplyTo", String.class);
                    if (jmsReplyTo == null) {
                        jmsReplyTo = endpoint.getReplyTo();
                    }
                }
            }

            // we must honor these special flags to preserve QoS
            // as we are not OUT capable and thus do not expect a reply, and therefore
            // the consumer of this message should not return a reply so we remove it
            // unless we use preserveMessageQos=true to tell that we still want to use JMSReplyTo
            if (jmsReplyTo != null && !(endpoint.isPreserveMessageQos() || endpoint.isExplicitQosEnabled())) {
                // log at debug what we are doing, as higher level may cause noise in production logs
                // this behavior is also documented at the camel website
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Disabling JMSReplyTo: " + jmsReplyTo + " for destination: " + to
                            + ". Use preserveMessageQos=true to force Camel to keep the JMSReplyTo on endpoint: "
                            + endpoint);
                }
                jmsReplyTo = null;
            }

            // the reply to is a String, so we need to look up its Destination instance
            // and if needed create the destination using the session if needed to
            if (jmsReplyTo != null && jmsReplyTo instanceof String) {
                // must normalize the destination name
                String replyTo = normalizeDestinationName((String) jmsReplyTo);
                // we need to null it as we use the String to resolve it as a Destination instance
                jmsReplyTo = null;

                // try using destination resolver to lookup the destination
                if (endpoint.getDestinationResolver() != null) {
                    jmsReplyTo = endpoint.getDestinationResolver().resolveDestinationName(session, replyTo,
                            endpoint.isPubSubDomain());
                }
                if (jmsReplyTo == null) {
                    // okay then fallback and create the queue
                    if (endpoint.isPubSubDomain()) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Creating JMSReplyTo topic: " + replyTo);
                        }
                        jmsReplyTo = session.createTopic(replyTo);
                    } else {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Creating JMSReplyTo queue: " + replyTo);
                        }
                        jmsReplyTo = session.createQueue(replyTo);
                    }
                }
            }

            // set the JMSReplyTo on the answer if we are to use it
            Destination replyTo = null;
            if (jmsReplyTo instanceof Destination) {
                replyTo = (Destination) jmsReplyTo;
            }
            if (replyTo != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Using JMSReplyTo destination: " + replyTo);
                }
                answer.setJMSReplyTo(replyTo);
            } else {
                // do not use JMSReplyTo
                answer.setJMSReplyTo(null);
            }

            return answer;
        }
    };

    doSend(false, destinationName, destination, messageCreator, null);

    // after sending then set the OUT message id to the JMSMessageID so its identical
    setMessageId(exchange);

    // we are synchronous so return true
    callback.done(true);
    return true;
}