Avatar billede fredand Forsker
25. marts 2008 - 19:18 Der er 1 kommentar og
1 løsning

Why does my routing params in JMS turn out to be null?

Hello!

I try to set a param with the method setStringProperty("x", "y").
But when I try to read it, during consuming, with getStringProperty("x), I just get "null".
I really hope that some one can help me out with this problem.

I send a JMS message to a local queue at my MQ with the following code:

public static void send() throws Exception
{
System.out.println("Start");
Properties properties = new Properties();
properties.setProperty("java.naming.factory.initial", "com.sun.jndi.fscontext.RefFSContextFactory");
properties.setProperty("java.naming.provider.url", "file:C://Fredrik//Eclipse_workspace//distribution//resources");
Context ctxNaming = new InitialContext(properties);
QueueConnectionFactory factory = (QueueConnectionFactory) ctxNaming.lookup("queue.connection.factory");
QueueConnection connection = factory.createQueueConnection();
connection.start();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctxNaming.lookup("MYQUEUE");
QueueSender sender = session.createSender(queue);
BytesMessage responseMessage = session.createBytesMessage();
responseMessage.writeBytes("hubba".getBytes());

responseMessage.setStringProperty("testkey", "testvalue");
System.out.println("\n test: " + responseMessage.getStringProperty("testkey") );

sender.send(responseMessage);
session.close();
connection.close();
System.out.println("Stop");
}

It looks like the param get set since:
System.out.println("\n test: " + responseMessage.getStringProperty("testkey") );
...prints out:
test: testvalue

But when I read and consume the message with the following code:
public static void read() throws Exception
{
System.out.println("Start");
Properties properties = new Properties();
properties.setProperty("java.naming.factory.initial", "com.sun.jndi.fscontext.RefFSContextFactory");
properties.setProperty("java.naming.provider.url", "file:C://Fredrik//Eclipse_workspace//distribution//resources");
Context ctxNaming = new InitialContext(properties);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctxNaming.lookup("queue.connection.factory");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.start();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (javax.jms.Queue) ctxNaming.lookup("MYQUEUE");
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
Message message = queueReceiver.receiveNoWait();

if(message instanceof TextMessage)
{
logger.debug("\n"+((TextMessage)message).getText() );
}
if(message instanceof BytesMessage)
{
System.out.println("\n test: " + message.getStringProperty("testkey") );
System.out.println("\n test: " + ((BytesMessage)message).getStringProperty("testkey") );
}
else
{
logger.debug("\n"+message.toString());
}

queueSession.close();
queueConnection.close();
System.out.println("Stop");
}

I just get: 
test: null.

I really can not figure out why?

So if any one got an idea or clue, please let me know.

Best regards
Fredrik
Avatar billede fredand Forsker
25. marts 2008 - 20:40 #1
Hello!

I do not nearly believe my self, but I think I found the error.

When I set up the jndi I set TARGCLIENT(MQ).

I found out that it probably should be TARGCLIENT(JMS)

Then it looks that I can read all setXXXProperties.

Best regards
Fredrik
Avatar billede fredand Forsker
25. marts 2008 - 20:40 #2
svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester