ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ Java / JMS » Reading JMS-Message with a standalone Java-Program

Post new topic  Reply to topic
 Reading JMS-Message with a standalone Java-Program « View previous topic :: View next topic » 
Author Message
hueso
PostPosted: Wed Nov 30, 2005 7:36 am    Post subject: Reading JMS-Message with a standalone Java-Program Reply with quote

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
View user's profile Send private message
jefflowrey
PostPosted: Wed Nov 30, 2005 7:52 am    Post subject: Reply with quote

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
View user's profile Send private message
gorilla
PostPosted: Wed Nov 30, 2005 8:31 am    Post subject: Reply with quote

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
View user's profile Send private message
jefflowrey
PostPosted: Wed Nov 30, 2005 8:49 am    Post subject: Reply with quote

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
View user's profile Send private message
hueso
PostPosted: Thu Dec 01, 2005 12:34 am    Post subject: Reply with quote

Newbie

Joined: 30 Nov 2005
Posts: 3

Thanks for the answers! I think, withs these clues, I can make it. Thanks!
Back to top
View user's profile Send private message
hueso
PostPosted: Thu Dec 01, 2005 3:33 am    Post subject: Reply with quote

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
View user's profile Send private message
gorilla
PostPosted: Tue Dec 06, 2005 1:26 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ Java / JMS » Reading JMS-Message with a standalone Java-Program
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.