Pub-Sub Messaging using Topics
This tutorial will get you started with Topics in the Message Broker profile (MB profile) of WSO2 Enterprise Integrator (WSO2 EI). This will also introduce the concepts of Hierarchical Topics, and Durable Subscriptions.
Prerequisites
Install WSO2 EI and download JMS clients as explained below.
Download and start the MB profile of WSO2 EI
Follow the steps given below to download and start the MB profile of WSO2 EI.
Download the product installer from here, and run the installer.
Start the MB profile:
The URL of the management console will be printed in the terminal as follows: https://localhost:9446/carbon/
Download and set up Apache JMeter
We will be using Apache JMeter for the purpose of simulating how a JMS client sends messages to the MB profile, and how such a client subscribes to topics and queues in the broker. Therefore, let us install and start Apache JMeter by following the steps given below.
Download Apache JMeter from here and install it. The location of your JMeter folder will be referred to as
<JMeter_HOME>from hereon.Copy the following JARs from the
<EI_HOME>/wso2/broker/client-libdirectory to the<JMeter_Home>/libfolder:andes-client-3.3.11.jargeronimo-jms_1.1_spec-1.1.0.wso2v1.jarlog4j-1.2.16.jarorg.wso2.securevault-1.1.3.jar
In the following steps, we will go through the basics of creating topics in the MB profile by creating a simple hierarchy of topics and then setting up JMS clients as the subscriber and publisher for the topics.
Step 1: Creating the topic hierarchy
Let's start by creating the following hierarchy:
To add the topics and subtopics:
Log in to the management console using the credentials of the default system administrator: admin/admin.
In the Main tab, click Topics -> Add. The Add Topic screen will open.
Enter 'games' as the topic name in the Topic field.
Click Add Topic and the new topic will be listed in the Topic List page. You can access the Topic List page by clicking Topic -> List in the Main tab of the navigator.
Click games in the topic list. This will open the following options:
Click Add Subtopic to open the Add Sub Topic screen.
Enter 'football' as the subtopic name and click Add Topic.
Repeat the steps to add the remaining subtopics until you get the complete topics hierarchy.
Step 2: Creating the jnditopics.properties file
Create the jnditopics.properties file shown below and save it to a location in your system. The details in this file will establish the connection between your JMeter clients and the required topics in the Message Broker instance.
# register some connection factories
# connectionfactory.[jndiname] = [ConnectionURL]
connectionfactory.TopicConnectionFactory = amqp://admin:admin@clientID/carbon?brokerlist='tcp://localhost:5675'
# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.All_Games = games.#
topic.All_Cricket = games.cricket.#
topic.All_Football = games.football.#
topic.Games_Only = games
topic.Cricket_Only = games.cricket
topic.Football_Only = games.football
topic.Germany_Only = games.football.germany
topic.Brazil_Only = games.football.brazil
topic.Australia_Only = games.cricket.australia
topic.India_Only = games.cricket.indiaNote the following:
We are using
localhostas the IP address, which is the same IP we used for starting the management console of the MB profile.The physical name of the topics are mapped to JNDI names according to the topic hierarchy. The following table describes the mapping:
Step 3: Creating a non-durable subscription to topics
What are non-durable subscriptions?
When a subscriber client uses a non-durable subscription, the client can receive messages only when the subscriber-broker connection is active. Read more about durable/non-durable topic subscriptions.
We will now set up Apache JMeter as a JMS subscriber for the topics we created in the previous step. For this tutorial, we will create a subscription for All Topics in the topic hierarchy.
Follow the instructions given below.
Start a new JMeter instance by executing the following script from the
<JMeter_Home>/binfolder:On windows:
jmeter.batOn Linux:
sh jmeter.sh
Go to the JMeter UI and create a JMS subscriber as explained below.
Create a new thread group: Right-click Test Plan (in the left navigator) and go to Add -> Threads (Users) -> Thread Group.
The new thread group will now be listed in the left navigator under the Test Plan.
Right-click the new thread group and go to Add -> Sampler -> JMS Subscriber.
Name this subscriber
Games Subscriber.You will now have a JMS Subscriber node in the left navigator. Select this node and enter the following details:
The connection details you specified in the
jnditopics.propertiesfile should be entered for the JMS Subscriber as shown below.Right-click the new Thread Group and go to Add -> Listener -> View Results Tree. This will create a listener node in the JMeter navigator for analysing the output of the subscription.
Save the information. You now have a JMS client subscribed to the 'games' topic and all its child topics.
Execute the JMeter client.
Now, go to the management console of the Message Broker profile and go to Main -> Subscriptions -> Topic Subscription List. Your subscription will be listed as a non-durable topic subscription:
Step 4: Creating durable subscriptions to a topic
What are durable subscriptions?
When a subscriber client with a durable subscription connects to the broker, it will receive all messages that were published for the subscription (even while the subscriber-broker connection was inactive). Read more about durable/non-durable topic subscriptions.
Let's take two JMeter clients and create two separate durable subscriptions. For this tutorial, we will create subscriptions for All Topics in the topic hierarchy.
Open two separate instances of JMeter by executing the following script from the
<JMeter_Home>/binfolder:On windows:
jmeter.batOn Linux:
sh jmeter.sh
Create JMS subscribers in both JMeter instances. Name them JMS Durable Subscriber 01 and JMS Durable Subscriber 2 respectively. Use the following connection details (same as for the non-durable subscription).
In addition to the above configuations, be sure to add durable subscription IDs for both subscribers. Let's use Sub_1 and Sub_2 to identify the subscriptions.
For JMS Durable Subscriber 01:
For JMS Durable Subscriber 02:
Right-click the new Thread Group and go to Add -> Listener -> View Results Tree. This will create a listener node in the JMeter navigator for analysing the output of the subscription.
Save the information.
Execute both the JMeter instances.
Now, go to the management console of the Message Broker profile and go to Main -> Subscriptions -> Topic Subscription List. The durable subscriptions will be listed as active durable subscriptions:
Now, open the JMeter instance for
JMS Durable Subscriber 02and stop the execution.Now, the disconnected durable subscriber will be listed as an inactive durable subscription in the management console as shown below.