1 )Create a topic
Add the following in the deploymessagingdestinations-service.xml file
<mbean code="org.jboss.jms.server.destination.TopicService" name="jboss.messaging.destination:service=Topic,name=Topic1" xmbean-dd="xmdesc/Topic-xmbean.xml"> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends> <depends>jboss.messaging:service=PostOffice</depends> </mbean>
2) Copy the TopicSend and TopicReceive programs to a folder.
TopicSend
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.PortableRemoteObject;public class TopicSend
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY=”org.jnp.interfaces.NamingContextFactory”;
// Defines the JMS connection factory.
public final static String JMS_FACTORY=”ConnectionFactory”;
// Defines the topic.
public final static String TOPIC=”/topic/Topic1″;protected TopicConnectionFactory tconFactory;
protected TopicConnection tcon;
protected TopicSession tsession;
protected TopicPublisher tpublisher;
protected Topic topic;
protected TextMessage msg;public void init(Context ctx, String topicName)
throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory)
PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),
TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);
tpublisher = tsession.createPublisher(topic);
msg = tsession.createTextMessage();
tcon.start();
}public void send(String message) throws JMSException {
msg.setText(message);
tpublisher.publish(msg);
}public void close() throws JMSException {
tpublisher.close();
tsession.close();
tcon.close();
}public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println(“Usage: java TopicSend jboss_url”);
return;
}
InitialContext ic = getInitialContext(args[0]);
TopicSend ts = new TopicSend();
ts.init(ic, TOPIC);
readAndSend(ts);
ts.close();
}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): n”);
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”));
}protected static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);return new InitialContext(env);
}}
TopicReceive
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.PortableRemoteObject;public class TopicReceive implements MessageListener {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY=”org.jnp.interfaces.NamingContextFactory”;
// Defines the JMS connection factory for the topic.
public final static String JMS_FACTORY=”ConnectionFactory”;
// Defines the topic.
public final static String TOPIC=”/topic/Topic1″;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
*/
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) {
System.err.println(“An exception occurred: “+jmse.getMessage());
}
}public void init(Context ctx, String topicName)
throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory)
PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),
TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic)
PortableRemoteObject.narrow(ctx.lookup(topicName),
Topic.class);
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();
}public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println(“Usage: java TopicReceive jboss_url”);
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<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);return new InitialContext(env);
}}
3) Start your Jboss Server
D:JBossEnterprisePlatform-5.1.1_Newjboss-eap-5.1jboss-asbin>run -c server1 -b 0.0.0.0
4) Open two command prompts, compile and run the two programs as follows.
C:JBossLABS>set classpath=D:JBossEnterprisePlatform-5.1.1_Newjboss-eap-5.1jboss-asclient*;.;
C:JBossLABS>javac TopicReceive.java
C:JBossLABS>java TopicReceive jnp://localhost:1099
C:JBossLABS>set classpath=D:JBossEnterprisePlatform-5.1.1_Newjboss-eap-5.1jboss-asclient*;.;
C:JBossLABS>javac TopicSend.java
C:JBossLABS>java TopicSend jnp://localhost:1099
You can have multiple receivers.
Please let us know if you face any issues.
Cheers!
Wonders Team
Hi…can u say,,in jboss EAP 6.3 step by step by configartion of domain…
sure we will write an article on the same soon..