1
votes

Hi I want to connect and send a JMS message from my JSP to a JMS server which I had intalled as part of Tibco installation. Now by browsing through various stuff on internet I know how to send the message from JAVA to a JMS queue but the problem is I don't know hoe to connect to JMS server itself. Can any one please help me in this. Thanks

3

3 Answers

2
votes

You need to configure it on a Java EE app server - WebLogic, JBOSS, Glassfish, etc.

If you deploy your JSP on Tomcat or Jetty, and don't use a full-fledged Java EE app server, you'll have to add a JMS module to it - look for ActiveMQ or RabbitMQ or OpenJMS.

1
votes

Basically you need to get a connection factory by looking it up in the JNDI directory, all the other objects are created from that connection factory.

This is an example (from the JBoss docs) showing how to create a topic session:

InitialContext iniCtx = new InitialContext();
Object tmp = iniCtx.lookup("ConnectionFactory");
TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
conn = tcf.createTopicConnection();
topic = (Topic) iniCtx.lookup("topic/testTopic");
session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
conn.start();

More examples here

0
votes

This is an old topic, but maybe it can help.

To send a message from the user interface (clicking on a button) you need to map the button click event to the Java side. Then you can write a java code that sends a message to the JMS.

To send a message to a JMS queue actually need the followings:

Context context = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue;

And this is how it works:

context = getContext(host, port, user, password);
queueConnection = getConnectionFactory(context, connectionFactoryJndi);
queueSession = getQueueSession(queueConnection);
queue = getQueue(context, queueJndi);

// send a text message
queueConnection.start();
String message = "hello";
sendMessageToQueue(verbose, message, queueSession, queue);
queueConnection.stop();

To obtain the context you need to connect to the server:

private Context getContext(String host, int port, String user, String password) throws NamingException {
    String url = String.format("%s://%s:%d", protocol, host, port);

    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_CREDENTIALS, password);
    return new InitialContext(env);
}

Get the connection factory:

private QueueConnection getConnectionFactory(Context context, String jndiName)
        throws NamingException, JMSException {

    QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(jndiName);
    return connectionFactory.createQueueConnection();
}

Open a queue session:

private QueueSession getQueueSession(QueueConnection queueConnection) throws JMSException {
    return queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
}

Get the queue:

private Queue getQueue(Context context, String jndiName) throws NamingException {
    return (Queue) context.lookup(jndiName);
}

And finally, send your message to the queue:

private static void sendMessageToQueue(boolean verbose, String message, QueueSession queueSession, Queue queue) throws JMSException {

TextMessage textMessage = queueSession.createTextMessage(message);

try (QueueSender queueSender = queueSession.createSender(queue)) {
    queueSender.send(textMessage);
}

}

These code snippets come from here: https://github.com/zappee/jms-message-sender This is a JMS sender command-line tool, you can use this project as an example.

Hope that it helps.