Posted By:
Daniel_Hofmann
Posted On:
Thursday, March 14, 2002 01:29 PM
I have two programs, namePublisher.java and nameSubscriber.java. namePublisher creates a temporary topic, passes a message to a topic "name", which is then delivered nameSubscriber. nameSubscriber then delivers a message back to the temporary topic correctly --no errors! However, namePublisher, which has been waiting to receive a message from the temporary topic, continues to wait and never in fact does receive the message. It just sits there, waiting, doing nothing... Can someone please tell me what is wrong. Below is my code and output. I know the message is sent successfully from nameSubscriber to the temporary topic because (1), I have printed out the ID for the temporary topic in both programs and the
More>>
I have two programs, namePublisher.java and nameSubscriber.java. namePublisher creates a temporary topic, passes a message to a topic "name", which is then delivered nameSubscriber.
nameSubscriber then delivers a message back to the temporary topic correctly --no errors!
However, namePublisher, which has been waiting to receive a message from the temporary topic, continues to wait and never in fact does receive the message.
It just sits there, waiting, doing nothing... Can someone please tell me what is wrong. Below is my code and output.
I know the message is sent successfully from nameSubscriber to the temporary topic because (1), I have printed out the ID for the temporary topic in both programs and they match.(2) If I deliberatly alter my code in namePublisher, so I close the connection (therefore killing the temporary topic) before nameSubscriber sends its reply, I do get an error.
Thank you. I would appreciate all the help I an get.
Sincerely,
Daniel Hofmann
DHofmann@Charlesjones.com
OUTPUT
webdevl:~/java_work/JMS/examples$ java
namePublisher
Java(TM) Message Service 1.0.2
Reference Implementation (build b14)
Published message: asdas
(it hangs here, never returns...)
webdevl:~/java_work/JMS/examples$ java
nameSubscriber
Java(TM) Message Service 1.0.2
Reference Implementation (build b14)
Waiting for message.....
RECEIVED MESSAGE IN LISTENER
The Message Was = asdas
Getting ready to send reply to
com.sun.jms.Topic:
tempTopic_1__webdevl_1016139075327_4
Sent Message Successfully!
webdevl:~/java_work/JMS/examples$
import javax.jms.*;
import javax.naming.*;
import java.lang.Thread.*;
public class namePublisher {
public static void main(String[] args) {
InitialContext ic = null;
TopicConnectionFactory tcf1 = null;
TopicConnection tc1 = null;
TopicSession pubSession1 = null;
TopicPublisher topicPublisher1 = null;
Topic pTopic = null;
TemporaryTopic replyTopic1 = null;
TopicSession subSession1 = null;
TopicSubscriber topicSubscriber1 = null;
TextMessage message = null;
//I. Create a JNDI
InitialContext object.
try {
ic = new InitialContext();
} catch (NamingException e) {
System.err.println("Could not create JNDI context: " + e.toString());
e.printStackTrace();
System.exit(1);
}
//II. Look up connection
try {
tcf1 = (TopicConnectionFactory) ic.lookup("TopicConnectionFactory");
pTopic = (Topic) ic.lookup("name");
} catch (NamingException e) {
System.err.println("JNDI lookup failed: " + e.toString());
e.printStackTrace();
System.exit(1);
}
//III. Create connections and sessions
try {
// Create two TopicConnections.
tc1 = tcf1.createTopicConnection();
// Create TopicSessions for publishers.
pubSession1 = tc1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
// Create temporary topics for replies.
replyTopic1 = pubSession1.createTemporaryTopic();
// Create TopicSessions for subscribers.
subSession1 = tc1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
// Create subscribers, set message listeners, and start connections.
topicSubscriber1 = subSession1.createSubscriber(replyTopic1);
TextListener textListener = new TextListener();
topicSubscriber1.setMessageListener(textListener);
tc1.start();
// Create publishers.
topicPublisher1 = pubSession1.createPublisher(pTopic);
message = pubSession1.createTextMessage("asdas");
message.setJMSReplyTo(replyTopic1);
topicPublisher1.publish(message);
System.out.println("Published message: " + message.getText());
while (textListener.getMessageReceived() == false){
try{
Thread.sleep(2000);
}
catch
(InterruptedException e) {
}
}
}
catch(Exception e) {
System.out.println ("exception " + e.toString());
}
try {
tc1.close();
} catch (Exception e) {
System.err.println("Error closing " +
"connection 1: " + e.toString());
}
}
}
import javax.jms.*;
import javax.naming.*;
import java.io.*;
import java.lang.Thread.*;
public class nameSubscriber {
//Main method.
public static void main(String[] args) {
String topicName = null;
Context jndiContext = null;
TopicConnectionFactory topicConnectionFactory = null;
TopicConnection topicConnection = null;
TopicSession topicSession = null;
Topic topic = null;
TopicSubscriber topicSubscriber = null;
TextListener topicListener = null;
TextMessage message = null;
topicName = "name";
/*
* Create a JNDI InitialContext
object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI " +
"context: " + e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Look up connection factory
and topic. If either does
* not exist, exit.
*/
try {
topicConnectionFactory = (TopicConnectionFactory)
jndiContext.lookup("TopicConnectionFactory");
topic = (Topic) jndiContext.lookup(topicName);
} catch (NamingException e) {
System.out.println("JNDI lookup failed: " +
e.toString());
e.printStackTrace();
System.exit(1);
}
try {
topicConnection =
topicConnectionFactory.createTopicConnection();
topicSession =
topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
topicSubscriber =
topicSession.createSubscriber(topic);
topicListener = new TextListener();
topicSubscriber.setMessageListener(topicListener);
topicConnection.start();
System.out.println ("Waiting for message.....");
while (topicListener.getMessageReceived() == false){
try{
Thread.sleep(2000);
}
catch
(InterruptedException e) {
}
}
//Get the message from the listener
System.out.println ("The Message Was = " + topicListener.getMessageText());
//look up a matching string in the database using the message as an index
String dbValue
= "fakereturnvalue";
//obtain the message object from the message listener
Message objMessage = topicListener.getMessageObj();
//we are done with the even listener, so we close it off
topicConnection.close();
nameSubscriber ns = new nameSubscriber();
//Send a return message
ns.returnMessage
(objMessage, dbValue);
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
}
}
private void returnMessage(Message objMessage, String smessage) {
Context replyJndiContext = null;
TopicConnectionFactory replyTopicConnectionFactory = null;
TextMessage msg = null;
TopicConnection replyTopicConnection = null;
TopicSession replyTopicSession = null;
TopicPublisher replyTopicPublisher = null;
TextMessage replyMsg = null;
// Create a JNDI InitialContext
try {
replyJndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI context: " + e.toString());
e.printStackTrace();
System.exit(1);
}
// Look up connection factory and topic. If either does
// not exist, exit.
try {
replyTopicConnectionFactory = (TopicConnectionFactory) replyJndiContext.lookup("TopicConnectionFactory");
} catch (NamingException e) {
System.out.println("JNDI lookup failed: " + e.toString());
e.printStackTrace();
System.exit(1);
}
try {
System.out.println ("Getting ready to send reply to " + objMessage.getJMSReplyTo() );
replyTopicConnection = replyTopicConnectionFactory.createTopicConnection();
replyTopicSession = replyTopicConnection.createTopicSession(true, 0);
replyTopicPublisher = replyTopicSession.createPublisher((Topic)objMessage.getJMSReplyTo());
replyMsg = replyTopicSession.createTextMessage(smessage);
replyTopicPublisher.publish(replyMsg);
System.out.println ("Sent Message Successfully!");
replyTopicConnection.close();
}
catch (JMSException e) {
System.err.println("JMSException: " + e.toString());
} catch (Throwable te) {
System.err.println(" Exception: " + te.toString());
}
}
}
import javax.jms.*
public class TextListener implements MessageListener {
private boolean smessageReceived=false;
private String smessage="";
private Message objMessage=null;
//param message = the incoming message
public void onMessage(Message message) {
System.out.println ("RECEIVED MESSAGE IN LISTENER");
TextMessage msg = null;
try {
if (message instanceof
TextMessage) {
msg = (TextMessage) message;
setMessageText(msg);
setMessageObj(msg);
setMessageReceived(true);
} else {
System.out.println("Message of wrong type: " +
message.getClass().getName());
}
} catch (Throwable t) {
System.out.println("Exception in onMessage():" +
t.getMessage());
}
}
private void setMessageObj(Message message) {
objMessage = message;
}
public Message getMessageObj(){
return (objMessage);
}
private void setMessageReceived(boolean messageReceived){
smessageReceived = messageReceived;
}
public boolean getMessageReceived()
{
return smessageReceived;
}
private void setMessageText(TextMessage message) {
try{
smessage = message.getText();
} catch (JMSException e) {
System.out.println("JMSException in onMessage(): " +
e.toString());
}
}
public String getMessageText() {
return smessage;
}
}
<<Less