• Post Reply Bookmark Topic Watch Topic
  • New Topic

JMS producer consumer example  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 555
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to create a simple JMS producer consumer using solace messaging. I guess the API is not very famous, but I am trying a generic implementation. I am trying to create two endpoints, which will listen on one topic and send message on another. The issue comes with keeping the consumer alive to listen. I know I am missing something very basic here. Here is the code:



Would someone please point out what is it that I am doing wrong here?
 
Tim Moores
Saloon Keeper
Posts: 3575
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "the issue"? How are you using this code?

You might also say a bit about "SolConnectionFactoryImpl", which is not a standard Java or JMS class.
 
s ravi chandran
Ranch Hand
Posts: 555
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:What is "the issue"? How are you using this code?

You might also say a bit about "SolConnectionFactoryImpl", which is not a standard Java or JMS class.

This is sample code. If this works, I will develop the actual class using this as base. SolConnectionFactoryImpl is JMS implementation from Solace Message Service. The issue, is once I start the connection, the program exits. For an async listener to get message, the consumer should remain active. I am not getting how to do so.
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say "the program exits" but I don't see any program in your post. I assume that somewhere you have some code which creates a MessageService object and uses it as a MessageListener, so if you're asking why that exits immediately then it would be helpful for us to see its code.
 
s ravi chandran
Ranch Hand
Posts: 555
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the missing part. It was a main method with these statements :



This is the code that was at the end of the class.
 
zlatan rahim
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Jdev, now create a simple producer and consumer.

a) Create a new Application and Project.

b) Create a simple producer class that will send 5 messages to the queue.

package project1;

import javax.jms.*;
import javax.naming.*;
import java.util.Hashtable;

public class QueueProducer {

public static void main(String[] args) {
String queueName = "jms/MyFirstQueue";
String queueConnectionFactoryName = "jms/MyFirstQCF";
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
int noMessages = 5;

/*
* Set the environment for a connection to the OC4J instance
*/
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
env.put(Context.SECURITY_CREDENTIALS, "welcome1");
env.put(Context.PROVIDER_URL, "opmnrmi://localhost:6007:JMS/default");

/*
* Set the Context Object.
* Lookup the Queue Connection Factory.
* Lookup the JMS Destination.
*/
try {
jndiContext = new InitialContext(env);
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(queueConnectionFactoryName);
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI lookup failed: " +
e.toString());
System.exit(1);
}

/*
* Create connection.
* Create session from connection.
* Create sender.
* Create text message.
* Send messages.
* Send non text message to end text messages.
* Close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < noMessages; i++) {
message.setText("Message " + (i + 1));
System.out.println("Producing message: " +
message.getText());
queueSender.send(message);
}
queueSender.send(queueSession.createBytesMessage());;
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
System.out.println("Closing error: " + e.toString());
}
}
}
}
}

c) Create a simple consumer class that will read all text messages from the queue and end once it reads a non text message:

package project1;

import java.util.Hashtable;

import javax.jms.*;

import javax.naming.*;

public class QueueConsumer {

public static void main(String[] args) {
String queueName = "jms/MyFirstQueue";
String queueConnectionFactoryName = "jms/MyFirstQCF";
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
TextMessage message = null;

/*
* Set the environment for a connection to the OC4J instance
*/
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
env.put(Context.SECURITY_CREDENTIALS, "welcome1");
env.put(Context.PROVIDER_URL,
"opmnrmi://localhost:6007:JMS/default");

/*
* Set the Context Object.
* Lookup the Queue Connection Factory.
* Lookup the JMS Destination.
*/
try {
jndiContext = new InitialContext(env);
queueConnectionFactory =
(QueueConnectionFactory)jndiContext.lookup(queueConnectionFactoryName);
queue = (Queue)jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI lookup failed: " + e.toString());
System.exit(1);
}

/*
* Create connection.
* Create session from connection.
* Create receiver.
* Receive all text messages from queue until non text message is receivied
* Close connection.
*/
try {
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage)m;
System.out.println("Consuming message: " +
message.getText());
} else {
break;
}
}
}
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
System.out.println("Closing error: " + e.toString());
}
}
}
}
}

d) Add the following libraries to the project:
* J2EE
* Apache Ant

5) Run the QueueProducer class to send the message to the queue.

The is result:
Producing message: Message 1
Producing message: Message 2
Producing message: Message 3
Producing message: Message 4
Producing message: Message 5

6) Run the QueueConsumer class to receive the message from the queue.

This is the result:

Consuming message: Message 1
Consuming message: Message 2
Consuming message: Message 3
Consuming message: Message 4
Consuming message: Message 5
 
s ravi chandran
Ranch Hand
Posts: 555
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the sample code. Here in the code, you are using a sync consumer. My requirement is actually an async consumer. Hence the use of a message listener. So, in place of using , i have used .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!