[JMS] Standalone JMS Client

If you were trying to make a JMS client to run as a standalone application

1. Create a java project.
2. Add as dependencies:

java-ee api and
C:installsglassfishglassfish-4.1glassfishlibgf-client.jar
(adjust path)
3. Create main class:
[code language=”java”]
package jmsclient;

import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import static javax.naming.Context.INITIAL_CONTEXT_FACTORY;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JmsClient {

//@Resource(mappedName = "jms/Factory")
private static ConnectionFactory connectionFactory;

//@Resource(mappedName = "jms/Queue")
private static Queue queue;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {

try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
Context initialContext = new InitialContext(props);

connectionFactory = (QueueConnectionFactory) initialContext.lookup("jms/Factory");
queue = (Queue) initialContext.lookup("jms/Queue");

} catch (NamingException ex) {
Logger.getLogger(JmsClient.class.getName()).log(Level.SEVERE, null, ex);
}

Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage();
message.setText("This is message 1");
messageProducer.send(message);

} catch (JMSException ex) {
ex.printStackTrace();
Logger.getLogger(JmsClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
[/code]

4. Create “jms/Factory” and “jms/Queue” JMS resources in Glassfish
5. Create and deploy your JMS EJB SimpleMessageBean

[code language=”java”]

package com.vvirlan.ejb;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(mappedName = "jms/Queue", activationConfig = {
@ActivationConfigProperty(propertyName = "aknowledgeMode",
propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue")
})
public class SimpleMessageBean implements MessageListener {

@Resource
private MessageDrivenContext mdc;

@Override
public void onMessage(Message message) {
TextMessage msg = null;
try {

if (message instanceof TextMessage) {
msg = (TextMessage) message;
Logger.getLogger(SimpleMessageBean.class.getName()).log(Level.SEVERE, null, msg.getText());
System.out.println(msg.getText());
}
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();;
}
}
}
[/code]

6. Run your client. You should see the message in the logs: This is message 1

Leave a Reply

Your email address will not be published. Required fields are marked *