This Post explains Topics in WSO2 Message Broker (MB) with Subscribing and Publishing.For this we will write two java clients.
  • TopicSubscriber.java to Subcribe for messages
  • TopicPublisher.java to to Publish the messages

Let's Start.

[1] Get WSO2 MB from http://wso2.com/products/message-broker/
[2] Create Porject "Client" on IDE that you preferred
[3] Add below to lib Dir in the project (Those jars can be found in Client Lib in MB)

  • andes-client-0.13.wso2v4.jar
  • geronimo-jms_1.1_spec-1.1.0.wso2v1.jar
  • log4j-1.2.17.jar
  • org.wso2.carbon.event.client-4.0.0.jar
  • org.wso2.carbon.event.client.stub-4.0.0.jar
  • slf4j-1.5.10.wso2v1.jar

[4] Creat class "TopicSubscriber.java" to Subcribe for messages

 
package simple;

import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicSubscriber {

 private String topicName = "news.sport";
 private String initialContextFactory = "org.wso2.andes.jndi."
+"PropertiesFileInitialContextFactory";
 private String connectionString = "amqp:"
+"//admin:admin@clientID/carbon?brokerlist='tcp://localhost:5672'";
 private boolean messageReceived = false;

 public static void main(String[] args) {
  TopicSubscriber subscriber = new TopicSubscriber();
  subscriber.subscribeWithTopicLookup();
 }

 public void subscribeWithTopicLookup() {

  Properties properties = new Properties();
  TopicConnection topicConnection = null;
  properties.put("java.naming.factory.initial", initialContextFactory);
  properties.put("connectionfactory.QueueConnectionFactory",
    connectionString);
  properties.put("topic." + topicName, topicName);
  try {
   InitialContext ctx = new InitialContext(properties);
   TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx
     .lookup("QueueConnectionFactory");
   topicConnection = topicConnectionFactory.createTopicConnection();
   System.out
     .println("Create Topic Connection for Topic " + topicName);

   while (!messageReceived) {
    try {
     TopicSession topicSession = topicConnection
       .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

     Topic topic = (Topic) ctx.lookup(topicName);
     // start the connection
     topicConnection.start();

     // create a topic subscriber
     javax.jms.TopicSubscriber topicSubscriber = topicSession
       .createSubscriber(topic);

     TestMessageListener messageListener = new TestMessageListener();
     topicSubscriber.setMessageListener(messageListener);

     Thread.sleep(5000);
     topicSubscriber.close();
     topicSession.close();
    } catch (JMSException e) {
     e.printStackTrace();
    } catch (NamingException e) {
     e.printStackTrace();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }

  } catch (NamingException e) {
   throw new RuntimeException("Error in initial context lookup", e);
  } catch (JMSException e) {
   throw new RuntimeException("Error in JMS operations", e);
  } finally {
   if (topicConnection != null) {
    try {
     topicConnection.close();
    } catch (JMSException e) {
     throw new RuntimeException(
       "Error in closing topic connection", e);
    }
   }
  }
 }

 public class TestMessageListener implements MessageListener {
  public void onMessage(Message message) {
   try {
    System.out.println("Got the Message : "
      + ((TextMessage) message).getText());
    messageReceived = true;
   } catch (JMSException e) {
    e.printStackTrace();
   }
  }
 }

}

 
[5] Creat class "TopicPublisher.java" to to Publish the messages
 
package simple;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class TopicPublisher {
 private String topicName = "news.sport";
 private String initialContextFactory = "org.wso2.andes.jndi."
 +"PropertiesFileInitialContextFactory";
 private String connectionString = "amqp:"
 +"//admin:admin@clientID/carbon?brokerlist='tcp://localhost:5672'";

 public static void main(String[] args) {
  TopicPublisher publisher = new TopicPublisher();
  publisher.publishWithTopicLookup();
 }

 public void publishWithTopicLookup() {
  Properties properties = new Properties();
  TopicConnection topicConnection = null;
  properties.put("java.naming.factory.initial", initialContextFactory);
  properties.put("connectionfactory.QueueConnectionFactory",
    connectionString);
  properties.put("topic." + topicName, topicName);

  try {
   // initialize
   // the required connection factories
   InitialContext ctx = new InitialContext(properties);
   TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx
     .lookup("QueueConnectionFactory");
   topicConnection = topicConnectionFactory.createTopicConnection();

   try {
    TopicSession topicSession = topicConnection.createTopicSession(
      false, Session.AUTO_ACKNOWLEDGE);
    // create or use the topic
    System.out.println("Use the Topic " + topicName);
    Topic topic = (Topic) ctx.lookup(topicName);
    javax.jms.TopicPublisher topicPublisher = topicSession
      .createPublisher(topic);

    String msg = "Hi, I am Test Message";
    TextMessage textMessage = topicSession.createTextMessage(msg);

      topicPublisher.publish(textMessage);
    System.out.println("Publishing message " +textMessage);
    topicPublisher.close();
    topicSession.close();

    Thread.sleep(20);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }

  } catch (JMSException e) {
   throw new RuntimeException("Error in JMS operations", e);
  } catch (NamingException e) {
   throw new RuntimeException("Error in initial context lookup", e);
  }
 }

}

 
[6] Firstly Run "TopicSubscriber.java" and then run "TopicPublisher.java"

Here is out put from both
TopicSubscriber::

Create Topic Connection for Topic news.sport
Got the Message : Hi, I am Test Message

TopicPublisher::

Use the Topic news.sport Publishing message
Body: Hi, I am Test Message
JMS Correlation ID: null
JMS timestamp: 1359720212306
JMS expiration: 0
JMS priority: 4
JMS delivery mode: 2
JMS reply to: null
JMS Redelivered: false
JMS Destination: topic://amq.topic/news.sport/?routingkey='news.sport'&exclusive='true'&autodelete='true'
JMS Type: null
JMS MessageID: ID:d7915d2c-6ddc-3b8a-b1aa-7a63009c6cae
JMS Content-Type: text/plain AMQ message number: -1
Properties:
 JMS_QPID_DESTTYPE = 2

 [More] Here is full message that we have send to TopicSubscriber. We can get that any parameter in above.
Here is sample to get TimeStamp and ID from JMS message.

 public class TestMessageListener implements MessageListener {
  public void onMessage(Message message) {
   try {
    System.out.println("Got the Message  TimeStamp: "
      +  message.getJMSTimestamp());
    System.out.println("Got the Message JMS ID : "
      +  message.getJMSMessageID());
    messageReceived = true;
   } catch (JMSException e) {
    e.printStackTrace();
   }
  }
 } 
 
1

View comments

  1. Hi,

    how can we het the topics list? I dont want to hard code my topic name... Is there a way to get list of all topics?

    Thanks.
    Naveen

    ReplyDelete

We used have  Singleton Design Pattern in our applications whenever it is needed. As we know that in singleton design pattern we can create only one instance and can access in the whole application. But in some cases, it will break the singleton behavior.

There are mainly 3 concepts which can break singleton property of a singleton class in java. In this post, we will discuss how it can break and how to prevent those.

Here is sample Singleton class and SingletonTest class.
13

Microservices can have a positive impact on your enterprise. Therefore it is worth to know that, how to handle Microservice Architecture (MSA) and some Design Patterns for Microservices. General goals or principles for a microservice architecture. Here are the four goals to consider in Microservice Architecture approach [1].

Reduce Cost: MSA will reduce the overall cost of designing, implementing, and maintaining IT services.
12

Last few years has been a great year for API Gateways and API companies. APIs (Application Programming Interfaces) are allowing businesses to expand beyond their enterprise boundaries to drive revenue through new business models. Larger enterprises are adopting API paradigm — developing many internal and external services that developers connect to in order to create user-facing products.
1

kubectl (Kubernetes command-line tool) is to deploy and manage applications on Kubernetes. Using kubectl, you can inspect cluster resources; create, delete, and update components.

NOTE

You must use a kubectl version that is within one minor version difference of your cluster. If not you may see errors as below

1.
2

WSO2 Enterprise Integrator is shipped with a separate message broker profile (WSO2 MB). In this Post I will be using message broker profile in EI (6.3.0).

1) Setting up the message broker profile

1.1) Copy the following JAR files from the <EI_HOME>/wso2/broker/client-lib/ directory to the <EI_HOME>/lib/ directory.
1

When two or many applications want to exchange data, they do so by sending the data through a channel that connects the each others. The application sending the data may not know which application will receive the data, but by selecting a particular channel to send the data on, the sender knows that the receiver will be one that is looking for that sort of data by looking for it on that channel.
2

Microservices are going completely over the enterprise and changed the way people write software within an enterprise ecosystem.

Let build you microservices with msf4j for Auto Mobile.

The SMPP inbound endpoint allows you to consume messages from SMSC via WSO2 ESB OR EI.

1.  Start SMSC

2.  Create custom inbound end point with below parameter. (Make sure you pick correct system-id and password correct for your SMSC)

3. Create Sequence for Inbound EP.

4. Once ESB or EI start.
1

WSO2 APIM Components

WSO2 API Manager includes five main components as the Publisher, Store, Gateway, Traffic Manager and Key Manager.

API Gateway - responsible for securing, protecting, managing, and scaling API calls. it intercepts API requests and applies policies such as throttling and security checks. It is also instrumental in gathering API usage statistics.
Loading