aspose file tools*
The moose likes BEA/Weblogic and the fly likes JMS messaging Topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "JMS messaging Topic" Watch "JMS messaging Topic" New topic
Author

JMS messaging Topic

jari bharat
Greenhorn

Joined: Jan 14, 2002
Posts: 12
Hello all,
here is step-by-step for creating JMS(Topic) messaging example.i have used code/explanation from BEA code examples so that u can better understand JMS API.

---------------------------------------------------------------------------------
1.Managing JMS :
To configure WebLogic JMS attributes, perform the following steps:

1.Start the Administration Console.
2.Select the JMS button under Services in the left pane to expand the list.
3.Follow the procedures described in the following sections
i) Configuring Connection Factories
Name : MyJMS Connection Factory
JNDIName : example.jms.TopicConnectionFactory
all others are default.
Target : your hostname.
ii) Configuring Stores
The backing store consists of a file or database that is used for persistent messaging.
in our case user "Configure a new JMSFile Store..."
ii) Configuring Servers
Name : MyJMSServer
Store : default
ii) Configuring Destination
Name : MyJMSTopic
JNDIName : example.jms.exampleTopic

---------------------------------------------------------------------------------

1. Application Development Flow
When developing a WebLogic JMS application, you must perform the steps identified in the following figure

i) Include the following package import statements at the beginning of your program:

import javax.jms.*;import java.util.*;import java.io.*;import javax.naming.*;import javax.transaction.*;
ii) Setting Up a JMS Application
Figure 2 Setting Up a JMS Application

--------------------------------------------------------------------
Classes Code
code for Topic Receiver.java
package example.jms;
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
/**
*
* This example shows how to establish a connection to and receive
* messages from a JMS topic. The classes in this package operate
* on the same JMS topic. Run the classes together to observe messages
* being sent and received. This class is used to receive messages from
* the topic.
* @author Copyright (c) 1999-2001 by BEA Systems, Inc. All Rights Reserved.
*/
public class TopicReceive
implements MessageListener
{
/**
* Defines the JNDI context factory.
*/
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
/**
* Defines the JMS connection factory for the topic.
*/
public final static String JMS_FACTORY="example.jms.TopicConnectionFactory";
/**
* Defines the topic.
*/
public final static String TOPIC="example.jms.exampleTopic";
private TopicConnectionFactory tconFactory;
private TopicConnection tcon;
private TopicSession tsession;
private TopicSubscriber tsubscriber;
private Topic topic;
private boolean quit = false;
/**
* Message listener interface.
* @param msg message
*
*/
// MessageListener interface
public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}
System.out.println("JMS Message Received: "+ msgText );
if (msgText.equalsIgnoreCase("quit")) {
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/**
* Creates all the necessary objects for sending
* messages to a JMS topic.
*
* @param ctx JNDI initial context
* @param topicNamename of topic
* @exception NamingExceptionif problem occurred with JNDI context interface
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String topicName)
throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ctx.lookup(topicName);
tsubscriber = tsession.createSubscriber(topic);
tsubscriber.setMessageListener(this);
tcon.start();
}
/**
* Closes JMS objects.
*
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close()
throws JMSException
{
tsubscriber.close();
tsession.close();
tcon.close();
}
/**
* main() method.
*
* @params args WebLogic Server URL
* @exception Exception if execution fails
*/
public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
System.out.println("Usage: java examples.jms.topic.TopicReceive WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
TopicReceive tr = new TopicReceive();
tr.init(ic, TOPIC);
System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
// Wait until a "quit" message has been received.
synchronized(tr) {
while (! tr.quit) {
try {
tr.wait();
} catch (InterruptedException ie) {}
}
}
tr.close();
}
private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
env.put("weblogic.jndi.createIntermediateContexts", "true");
return new InitialContext(env);
}
}
code for TopicSend.java
package example.jms;
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
/**
* This examples shows how to establish a connection and send messages to the
* JMS topic. The classes in this package operate on the same topic. Run
* the classes together to observe message being sent and received. This
* class is used to send messages to
* the topic.
*
* @author Copyright (c) 1999-2001 by BEA Systems, Inc. All Rights Reserved.
*/
public class TopicSend
{
/**
* Defines the JNDI context factory.
*/
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
/**
* Defines the JMS connection factory.
*/
public final static String JMS_FACTORY="example.jms.TopicConnectionFactory";
/**
* Defines the topic.
*/
public final static String TOPIC="example.jms.exampleTopic";
protected TopicConnectionFactory tconFactory;
protected TopicConnection tcon;
protected TopicSession tsession;
protected TopicPublisher tpublisher;
protected Topic topic;
protected TextMessage msg;
/**
* Creates all the necessary objects for sending
* messages to a JMS Topic.
*
* @param ctx JNDI initial context
* @param topicName name of topic
* @exception NamingExcpetion if problem occurred with the JNDI context interface
* @exception JMSException if JMS fails to initialize due to internal error
*
*/
public void init(Context ctx, String topicName)
throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ctx.lookup(topicName);
tpublisher = tsession.createPublisher(topic);
msg = tsession.createTextMessage();
tcon.start();
}
/**
* Sends a message to a JMS topic.
*
* @params message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*
*/
public void send(String message)
throws JMSException
{
msg.setText(message);
tpublisher.publish(msg);
}
/**
* Closes JMS objects.
*
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close()
throws JMSException
{
tpublisher.close();
tsession.close();
tcon.close();
}
/**
* main() method.
*
* @param args WebLogic Server URL
* @exception Exception if operation fails
*/
public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
System.out.println("Usage: java examples.jms.topic.TopicSend WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
TopicSend ts = new TopicSend();
ts.init(ic, TOPIC);
readAndSend(ts);
ts.close();
}
/**
* Prompts, reads, and sends a message.
*
* @param ts TopicSend
* @exception IOException if problem occurs during read/write operation
* @exception JMSException if JMS fails due to internal error
*/
protected static void readAndSend(TopicSend ts)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader (new InputStreamReader(System.in));
String line=null;
do {
System.out.print("Enter message (\"quit\" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
ts.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
}
} while (line != null && ! line.equalsIgnoreCase("quit"));
}
/**
* Get initial JNDI context.
*
* @params url Weblogic URL.
* @exception NamingException if problem occurs with JNDI context interface
*/
protected static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
env.put("weblogic.jndi.createIntermediateContexts", "true");
return new InitialContext(env);
}
}
----------------------------------------------------------------------------------------------
Application Deployment
1. create example/jms directory under WEB-INF/classes directory in your web application root:
complete dir structure :
app. root/WEB-INF/classes/example/jms
2.put both java class under this dir.
3. compile the code(TopicReceiver.java and TopicSend.java)
----------------------------------------------------------------------------
Run Application.
a. from command window type following command:
(for Receiver)classes>java example.jms.TopicReceive t3://hostname:7001
(for Sender)classes>java example.jms.TopicSend t3://hostname:7001

---------------------------------------------------------
that's all for JMS Topic Messaging.
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
That's a very good posting.
Keep up your great and helpful work.
Thanks again,
Kishore.


Kishore
SCJP, blog
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks, Jari, for all the examples that you have given us.
Anshu Acharya
Greenhorn

Joined: Oct 25, 2005
Posts: 1
The example's nice.
but I have a small doubt,
Is it possible to use a QueueSender to publish messages on to a Topic configured in weblogic or is a TopicPublisher the only way?

I'd appreciate any information on the same.
 
 
subject: JMS messaging Topic