Publish message on Topic destination in Oracle Weblogic Server 10.3.2

I have created two administrator objects on server: one Connection factory JNDI (cfjndi) and one Topic destination JNDI (tjndi1). There are three applications : one is sender and other two are subscriber and both are connected to the server.

Below steps are to create JMS Publisher and Subscriber :

  • Install Oracle WebLogic Server 10.3.2
  • Create Domain on Orcale WebLogic Server 10.3.2
  • Configure Domain in Eclipse
  • Create administrator objects for Topic on Server
  • Create Java project with Publisher and Subscriber
  • Run Subscribers and Publisher
Step 1: Install Oracle WebLogic Server 10.3.2 [ Click here - How to install WebLogic Server ]

Step 2: Create Domain on Orcale WebLogic Server 10.3.2 [ Click here - How to create Domain on WebLogic Server ]

Step 3: Configure Domain in Eclipse [ Click here - How to Configure Domain in Eclipse ]

Step 4: Create administrator objects for Topic on Server [ Click here - How to create JMS administrator objects on Server]

Step 5: Create Java project with Publisher and Subscriber
File -> New -> Project -> Java Project -> Next -> Specify Project name : JMSTopicClient -> Next
Active Library and Click Add External JARs and select weblogic.jar from WebLogic Server installation directory.

Create Sender : TopicPublisherClient.java
package techmyguru.queuclient;

// all imports

public class TopicSubscriberClient1 
{
public static void main(String args[]) 
{
System.out.println("Client-1 - Subscriber");
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
    "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
p.put(Context.SECURITY_PRINCIPAL, "weblogic");
p.put(Context.SECURITY_CREDENTIALS, "12345678");
InitialContext ctx = new InitialContext(p);
TopicConnectionFactory topicFactory = (TopicConnectionFactoryctx
    .lookup("cfjndi");
TopicConnection conn = topicFactory.createTopicConnection();
conn.start();
TopicSession session = conn.createTopicSession(false,
    TopicSession.AUTO_ACKNOWLEDGE);
Topic t = (Topicctx.lookup("tjndi1");
TopicSubscriber sub = session.createSubscriber(t);
sub.setMessageListener(new MyMessageListener());
while (true
{
Thread.sleep(1000);
}
catch (Exception e) {
System.out.println("error" + e);
}
}
}

Create Message Listener : MyMessageListener.java
package techmyguru.queuclient;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

class MyMessageListener implements MessageListener {
public void onMessage(Message msg) {
try {
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessagemsg;
String str = txtMsg.getText();
System.out.println("Message Received - " + str);
else
System.out.println("not appropriate message");
catch (Exception e) {
System.out.println("error in listener" + e);
}
}
}

Create Message Receiver : TopicSubscriberClient1.java
package techmyguru.queuclient;

// all imports

public class TopicSubscriberClient1 
{
public static void main(String args[]) 
{
System.out.println("Client-1 - Subscriber");
try
{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
    "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
p.put(Context.SECURITY_PRINCIPAL, "weblogic");
p.put(Context.SECURITY_CREDENTIALS, "12345678");
InitialContext ctx = new InitialContext(p);
TopicConnectionFactory topicFactory = (TopicConnectionFactoryctx
    .lookup("cfjndi");
TopicConnection conn = topicFactory.createTopicConnection();
conn.start();
TopicSession session = conn.createTopicSession(false,
    TopicSession.AUTO_ACKNOWLEDGE);
Topic t = (Topicctx.lookup("tjndi1");
TopicSubscriber sub = session.createSubscriber(t);
sub.setMessageListener(new MyMessageListener());
while (true) {
  Thread.sleep(1000);
}
catch (Exception e) {
System.out.println("error" + e);
}
}
}

Create Message Receiver : TopicSubscriberClient2.java
package techmyguru.queuclient;

import java.util.*;
import javax.jms.*;
import javax.naming.*;
public class TopicSubscriberClient2
{
public static void main(String args[])
{
  System.out.println("Client-2 - Subscriber");
try
{
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");
p.put(Context.SECURITY_PRINCIPAL, "weblogic");
p.put(Context.SECURITY_CREDENTIALS, "12345678");
InitialContext ctx=new InitialContext(p);
TopicConnectionFactory topicFactory=(TopicConnectionFactory)ctx.lookup("cfjndi");
TopicConnection conn=topicFactory.createTopicConnection();
conn.start();
TopicSession session=conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
Topic t=(Topic)ctx.lookup("tjndi1");
TopicSubscriber sub=session.createSubscriber(t);
sub.setMessageListener(new MyMessageListener());
while(true)
{
Thread.sleep(1000);
}
}
catch(Exception e)
{
System.out.println("error"+e);
}
}
}

Verify the project directory structure :
JMS Topic publisher

Run TopicSubscriberClient1.java , TopicSubscriberClient2.java and TopicPublisherClient respectively and get the output :
JMS Topic publisher
JMS Topic publisher
JMS Topic publisher
Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code