Example usage for javax.jms QueueConnectionFactory createQueueConnection

List of usage examples for javax.jms QueueConnectionFactory createQueueConnection

Introduction

In this page you can find the example usage for javax.jms QueueConnectionFactory createQueueConnection.

Prototype


QueueConnection createQueueConnection() throws JMSException;

Source Link

Document

Creates a queue connection with the default user identity.

Usage

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   ww w.ja  va  2 s .c  o  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.sdnmq.jms.FlowProgrammer.java

/**
 * JMS setup/* ww w .  j  a v a 2  s .co m*/
 */
private boolean 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 false;
    }

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

    try {
        connection = queueFactory.createQueueConnection();
    } catch (JMSException e) {
        log.error(e.getMessage());
        releaseMQ();
        return false;
    }

    try {
        session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    } catch (JMSException e) {
        log.error(e.getMessage());
        releaseMQ();
        return false;
    }

    String queueName = System.getProperty(FLOWPROGRAMMER_QUEUE_PROPERTY, DEFAULT_FLOWPROGRAMMER_QUEUE_NAME);
    log.info("Using the following queue for flow programming requests: " + queueName);
    try {
        flowProgrammerQueue = (Queue) ctx.lookup(queueName);
    } catch (NamingException e) {
        log.error(e.getMessage());
        releaseMQ();
        return false;
    }

    try {
        receiver = session.createReceiver(flowProgrammerQueue);
    } catch (JMSException e) {
        log.error(e.getMessage());
        releaseMQ();
        return false;
    }

    log.trace("Setup JMS successfully");

    return true;
}

From source file:org.dawnsci.commandserver.ui.view.StatusQueueView.java

protected Collection<StatusBean> getStatusBeans(final URI uri, final String queueName,
        final IProgressMonitor monitor) throws Exception {

    QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri);
    monitor.worked(1);//from   w  ww.  jav a2s .  c o  m
    QueueConnection qCon = connectionFactory.createQueueConnection(); // This times out when the server is not there.
    QueueSession qSes = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = qSes.createQueue(queueName);
    qCon.start();

    QueueBrowser qb = qSes.createBrowser(queue);
    monitor.worked(1);

    @SuppressWarnings("rawtypes")
    Enumeration e = qb.getEnumeration();

    Class clazz = getBeanClass();
    ObjectMapper mapper = new ObjectMapper();

    final Collection<StatusBean> list = new TreeSet<StatusBean>(new Comparator<StatusBean>() {
        @Override
        public int compare(StatusBean o1, StatusBean o2) {
            // Newest first!
            long t1 = o2.getSubmissionTime();
            long t2 = o1.getSubmissionTime();
            return (t1 < t2 ? -1 : (t1 == t2 ? 0 : 1));
        }
    });

    while (e.hasMoreElements()) {
        Message m = (Message) e.nextElement();
        if (m == null)
            continue;
        if (m instanceof TextMessage) {
            TextMessage t = (TextMessage) m;
            final StatusBean bean = mapper.readValue(t.getText(), clazz);
            list.add(bean);
        }
    }
    return list;
}

From source file:org.wso2.carbon.andes.core.QueueManagerServiceImpl.java

/**
 * Publish message to given JMS queue/*from   w  ww  .j a va2 s  .  co  m*/
 *
 * @param nameOfQueue queue name
 * @param userName username
 * @param accessKey access key
 * @param jmsType jms type
 * @param jmsCorrelationID message correlation id
 * @param numberOfMessages number of messages to publish
 * @param message message body
 * @param deliveryMode delivery mode
 * @param priority message priority
 * @param expireTime message expire time
 * @throws QueueManagerException
 */
private void send(String nameOfQueue, String userName, String accessKey, String jmsType,
        String jmsCorrelationID, int numberOfMessages, String message, int deliveryMode, int priority,
        long expireTime) throws QueueManagerException {
    QueueConnectionFactory connFactory;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    QueueSender queueSender = null;
    try {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, ANDES_ICF);
        properties.put(CF_NAME_PREFIX + CF_NAME, Utils.getTCPConnectionURL(userName, accessKey));
        properties.put(QUEUE_NAME_PREFIX + nameOfQueue, nameOfQueue);
        properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true");
        InitialContext ctx = new InitialContext(properties);
        connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        Queue queue = (Queue) ctx.lookup(nameOfQueue);
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        queueConnection.start();
        TextMessage textMessage = queueSession.createTextMessage();
        if (queueSender != null && textMessage != null) {
            if (jmsType != null) {
                textMessage.setJMSType(jmsType);
            }
            if (jmsCorrelationID != null) {
                textMessage.setJMSCorrelationID(jmsCorrelationID);
            }

            if (message != null) {
                textMessage.setText(message);
            } else {
                textMessage.setText("Type message here..");
            }

            for (int i = 0; i < numberOfMessages; i++) {
                queueSender.send(textMessage, deliveryMode, priority, expireTime);
            }
        }
    } catch (FileNotFoundException | NamingException | UnknownHostException | XMLStreamException
            | JMSException e) {
        throw new QueueManagerException("Unable to send message.", e);
    } finally {
        try {
            if (queueConnection != null) {
                queueConnection.close();
            }
        } catch (JMSException e) {
            log.error("Unable to close queue connection", e);
        }
        try {
            if (queueSession != null) {
                queueSession.close();
            }
        } catch (JMSException e) {
            log.error("Unable to close queue session", e);
        }
        try {
            if (queueSender != null) {
                queueSender.close();
            }
        } catch (JMSException e) {
            log.error("Unable to close queue sender", e);
        }
    }
}