Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm not able to receive message from JMS Topic  RSS feed

 
Vikas Sahu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm not able to receive message from JMS Topic even though message is posted on the JMS Topic by the sender. I'm able to see the messages received on the destination through the Weblogic console.

Any pointers welcome.

Regards,
Vikas

------------------------ complete sample code ---------------------------------
import javax.naming.*;
import javax.jms.*;

public class JMSMessageHandler
{
public final static String JMS_FACTORY =
"com.server.topic.TopicConnectionFactory";
public final static String TOPIC =
"com.server.ejb.JMSTest";

public JMSMessageHandler()
{
}

public void sendMessage(String msg)
{
TopicConnection tConnect = null;
Context jndiCtx = null;
try
{
jndiCtx = getInitialContext();
TopicConnectionFactory tFactory =
(TopicConnectionFactory) jndiCtx.lookup(JMS_FACTORY); //connectionFactory
Topic t = (Topic) jndiCtx.lookup(TOPIC); //dest
tConnect =
tFactory.createTopicConnection();
TopicSession session = tConnect.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = session.createPublisher(t);
TextMessage jmsMsg = session.createTextMessage();
// tConnect.start();
jmsMsg.setText(msg);
publisher.publish(jmsMsg);
print("Message sent to the topic:" + msg);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (tConnect != null)
{
tConnect.close();
}
}
catch (Exception e)
{}
}
}

public void receiveMessage()
{
TopicConnection tConnect = null;
Context jndiCtx = null;

try
{
jndiCtx = getInitialContext();
TopicConnectionFactory tFactory =
(TopicConnectionFactory) jndiCtx.lookup(JMS_FACTORY);

Topic t = (Topic) jndiCtx.lookup(TOPIC);
System.out.println(t.getTopicName());

tConnect =
tFactory.createTopicConnection();

TopicSession session = tConnect.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);

TopicSubscriber subscriber = session.createSubscriber(t);

tConnect.start();
System.out.println("Started receiving messages ....");
// long timeout = 10000; //time in milliseconds
while (true)
{
System.out.println("----12---");
TextMessage txt = (TextMessage)subscriber.receive(5);

if (txt != null)
print("Message Received from topic:" + txt.getText());
}


}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (tConnect != null)
{
tConnect.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

void print(String s)
{
System.out.println("MessageSender:" + s);
}

private Context getInitialContext() throws Exception
{
String url = "t3://" + ServerConfig.getServerIP() + ":" +ServerConfig.getServerPort(); // needs to change server IP and weblogic server port
String user = null;
String password = null;
Properties properties = null;
try
{
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null)
{
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}

return new InitialContext(properties);
}
catch (Exception e)
{
throw e;
}
}
public static void main(String[] args){

JMSMessageHandler jms = new JMSMessageHandler();
jms.sendMessage("Test Message");
jms.receiveMessage();
}

}
 
james edwin
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vikas,

run two separate program in two windows and try if you get an message from one window to another window.

Just try that !!! and Refer This i hope it helps !!
[ August 18, 2004: Message edited by: james edwin ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!