Author |
Message
|
hueso |
Posted: Wed Nov 30, 2005 7:36 am Post subject: Reading JMS-Message with a standalone Java-Program |
|
|
Newbie
Joined: 30 Nov 2005 Posts: 3
|
Hello, I'm a newbie in mq and jms. Here my question: I want to read a JMS-Message with a java standalone programm. At this time I am using classes form IBM (com.ibm.mq.*). The Problem is, that the Message-Typ is JMS an not MQ, so that the MQMessage-Object looks a very strange. |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Nov 30, 2005 7:52 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
You can use JMS in standalone programs.
You can use the plain Java MQ API to process or ignore the MQRFH2 header that is used with JMS messages.
You can instruct JMS to produce messages that do not have the MQRFH2 header in them.
You can find specific, indepth discussions of all of these three things in this forum.
The search button is your friend. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
gorilla |
Posted: Wed Nov 30, 2005 8:31 am Post subject: |
|
|
Novice
Joined: 17 Nov 2005 Posts: 16
|
You can use JMS directly without all the JNDI stuff if you want.
The following code fragments are pieced together from a program which sends a JMS message this way. I can't find any live code which reads message right now, but it's basically the same thing. The commented out lines for creating a receiver are probably right.
FWIW I think this was pre-JMS 1.1. The last time I wrote a program to read JMS messages I think I made a listener with an onMessage() method which got called every time a message was put in the queue. IIRC the setup is a bit different, but the code is a lot easier to write.
import javax.jms.*;
import com.ibm.mq.jms.MQQueueConnectionFactory;
QueueConnectionFactory factory = new MQQueueConnectionFactory();
((MQQueueConnectionFactory)factory).setQueueManager( "QMNAME" ) ;
QueueConnection connection = factory.createQueueConnection();
connection.start();
boolean transacted = false;
int ack = Session.AUTO_ACKNOWLEDGE ;
QueueSession session = null ;
session = connection.createQueueSession( transacted, ack) ;
Queue ioQueue = null ;
ioQueue = session.createQueue( "QNAME" ) ;
QueueSender queueSender = null ;
queueSender = session.createSender(ioQueue) ;
//QueueReceiver queueReceiver = null ;
//queueReceiver = session.createReceiver(ioQueue);
outMessage = session.createTextMessage();
outMessage.setText( "message payload" ) ;
getQueueSender().send(outMessage);
queueSender.close() ;
session.close();
connection.close();
Most or all of these calls can throw a JMSException. Here some simple catch code:
catch ( JMSException jmse) {
System.out.println( "\t" + jmse.getMessage() ) ;
}
You can also get the underlying MQ exception (or maybe the error message) from the JMSException. I can't find code for that, but I think I learned how to do it from an example in the WMQ Java coding manual.
Last edited by gorilla on Wed Nov 30, 2005 9:04 am; edited 1 time in total |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Nov 30, 2005 8:49 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
The underlying MQ Exception is included as a LinkedException in the JMSException... accessed through e.getLinkedException.
You should always write code to report this linkedException, as JMS Exceptions are very generic and loosely coupled to the problem. There could be a hundred different reasons why you can't create an instance of the connection, but the JMS error message will never tell you any more than that it couldn't be created. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
hueso |
Posted: Thu Dec 01, 2005 12:34 am Post subject: |
|
|
Newbie
Joined: 30 Nov 2005 Posts: 3
|
Thanks for the answers! I think, withs these clues, I can make it. Thanks! |
|
Back to top |
|
 |
hueso |
Posted: Thu Dec 01, 2005 3:33 am Post subject: |
|
|
Newbie
Joined: 30 Nov 2005 Posts: 3
|
@gorilla: You are using the method "getQueueSender()". Kan I have the Code of that method? |
|
Back to top |
|
 |
gorilla |
Posted: Tue Dec 06, 2005 1:26 am Post subject: |
|
|
Novice
Joined: 17 Nov 2005 Posts: 16
|
hueso - I'm really sorry about that. I always use acessor methods in my code, not direct references to object properties, but it makes pasted pieces of my programs unreadable to I change to locally created variable (that's why those "Type varName = null;" lines are there). Unfortunately I forgot one
Anyway, the line:
getQueueSender().send(outMessage);
should read:
queueSender.send(outMessage);
Once you've gone through that setup cascade, and you have a reference to a suitable sender or receiver, you are in the same place you get to after MQCONN + MQOPEN, and you can finally get to work  |
|
Back to top |
|
 |
|