简体   繁体   中英

How to initialize initial context in JMS

I would like to create a Message queue in a standalone application using JMS Queue. I am not using any kind of container like tomcat and JBoss. What should be the arguments passed to the initial context object.? It s completely a standalone application..

Note: If anybody wishes to give down vote for this question, please give the reason in the comment and give down vote. Thanks!

       InitialContext ctx = new InitialContext(?????);
       Queue queue = (Queue) ctx.lookup("queue/queue1");
       QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("queue/connectionFactory");
       QueueConnection queueConn = connFactory.createQueueConnection();
       QueueSession queueSession = queueConn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
       QueueSender queueSender = queueSession.createSender(queue);
       queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
       TextMessage message = queueSession.createTextMessage("Hello");
       queueSender.send(message);
       System.out.println("sent: " + message.getText());
       queueConn.close();

You can't resolve the connectionFactory via jndi as there are no container to provide it.

You will have to instantiate the connectionFactory yourself providing the necessary (transport) parameters.

As you don't retrieve it from a Java EE container this behavior is not covered by the related JSR and is so provider specific.

below an example using HornetQ :

// Transport parameters
final Map< String, Object > connectionParams = new HashMap< String, Object >();
connectionParams.put(TransportConstants.PORT_PROP_NAME, port);
connectionParams.put(TransportConstants.HOST_PROP_NAME, host);

final TransportConfiguration transportConfiguration = new TransportConfiguration(
    NettyConnectorFactory.class.getName(), connectionParams);

// this should be created only once and reused for the whole app lifecycle
connectionFactory = (ConnectionFactory) org.hornetq.api.jms.HornetQJMSClient
    .createConnectionFactoryWithoutHA(JMSFactoryType.QUEUE_CF, transportConfiguration);

final jmsQueue = HornetQJMSClient.createQueue(queueName)   

try {
    // connection is thread safe
    Connection connection = null;

    // session is not
    Session session = null;

    connection = connectionFactory.createConnection(user, password);
    connection.start();

   /* following objects must be propper to a thread (but should be reused if possible) */

    // Create a non transacted Session (no XA support outside of Java EE container)
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    final MessageProducer producer = session.createProducer(jmsQueue);
    final ObjectMessage objectMessage = session.createObjectMessage();

    objectMessage.setObject(myMessageSerializableObject);

    producer.send(objectMessage);
}
finally {
    // Release resources
    try {
        if (session != null) {
          session.close();
        }
        if (connection != null) {
          connection.close();
        }
    }
    catch (final JMSException e) {
        LOG.warn("An error occurs while releasing JMS resources", e);
    }
}

Note that connections, sessions and producers should be reused (not created and released for each use but not shared between threads ) and ideally pooled.

See https://developer.jboss.org/wiki/ShouldICacheJMSConnectionsAndJMSSessions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM