i struggled allot to make this JMS sample working .
follow the next instruction to use JMS under web sphere.
1. get into admin console
2. get into JMS->connection factories
-> new
choose web sphere MQ , click OK .
give it a logic name , and a jndi name such as jms/myMqFactory, and set the correct parameters during the wizard.
click finish and save.
3. goto Resources -> JMS ->queues
4. click new , and choose websphere MQ message provider
set the name and jndi name such as jms/mqRequestQ
and set the queue name
5. click apply and save.
some resources i used :
Developing a JMS client
IBM WebSphere Developer Technical Journal: Building an Enterprise Service Bus with WebSphere Application Server V6 -- Part 3
A java.lang.ClassCastException error occurs during a JNDI lookup of a queue connection factory
here is the code snippets, i used tp send message to mqRequestQ and get result from mqResponseQ , and pick the message with the same msg id:
private void testJMS(String qIn,String qOut )
{
String outString = "I am a string from JMS";
String qcfName = "jms/myMqFactory";
String qnameIn = "jms/mqRequestQ";
String qnameOut = "jms/mqResponseQ";
boolean verbose = false;
Session session = null;
Connection connection = null;
ConnectionFactory
qcf
= null;
Queue inQueue = null;
Queue outQueue = null;
InitialContext context = null;
try {
context = new InitialContext();
qcf = (ConnectionFactory)context.lookup( qcfName );
inQueue = (Queue)context.lookup( qnameIn );
outQueue = (Queue)context.lookup( qnameOut );
if(qcf != null)
{
connection = qcf.createConnection();
connection.start();
boolean transacted = false;
session = connection.createSession( transacted,
Session.AUTO_ACKNOWLEDGE);
((MQQueue)inQueue).setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ); //its 1
MessageProducer queueSender = session.createProducer(inQueue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
queueSender.setTimeToLive(60000); // in milli seconds 0 - unlimited
TextMessage outMessage = session.createTextMessage(outString);
outMessage.setJMSCorrelationID("Alis0000");
queueSender.send(outMessage);
queueSender.close();
String msgID = outMessage.getJMSMessageID();
String selector1 = "JMSMessageID='"+msgID+"'";
MessageConsumer queueReciever = session.createConsumer(outQueue, selector1);
javax.jms.Message inMessage = queueReciever.receive(2000);
if ( inMessage instanceof TextMessage )
{
String replyString = ((TextMessage) inMessage).getText();
}
queueReciever.close();
session.close();
session = null;
connection.close();
connection = null;
}
} catch (NamingException e) {
// there is no such DSN lets report it and will try to work without the DataSource name
} catch (JMSException je) {
System.out.println("JMS failed with "+je);
Exception le = je.getLinkedException();
if (le != null)
{
System.out.println("linked exception "+le);
}
}
}
enjoy
Yaniv Tzanany