Point-to-Point |
Publisher-Subscribers |
Reference : JavaTPoint. You can read more details of JMS from given reference.
JMS Flow
Now, Let me give some basic about middleware message broker in SOA/ESB based applications. Message Broker is the tools which is placed between producer and consumer to exchange the data using formal messaging protocol. You can have clear picture by referring below images which I found on RedHat portal. There are many provider of message brokers. The notable and popular brokers are ActiveMQ and RabbitMQ. Here, I have used ActiveMQ as middleware message broker. You can read more about ActiveMQ from here.
Reference: RedHat |
Step 1: To add JMS and ActiveMQ maven dependency in your project.
Add below lines of code in your projects pom.xml.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>x.x.x</version>
<!-- Your camel's version -->
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>x.x.x</version>
<!-- Check Apache Camel's version and find the appropriate activemq version -->
</dependency>
After adding this clean and build your project so, maven can load the dependent library in //users/.m2
Step 2: Create activemq.xml
Creates the activemq.xml in which you have to define the where to store the messages which comes to ActiveMQ. What are the broker name, what is the default directory for storage, what will be the persistence adapter for storage, memory usage etc. Place this file in the server classpath or conf directory from where the server looks up config related things.
\\server-base-dir\system\org\apache\activemq\activemq-karaf\{activemq-karaf-version}.redhat-{fuse-version}
broker-name=amq-broker
data=${karaf.data}/${broker-name}
config=${karaf.base}/etc/activemq.xml
Now, let's add the beans related to ActiveMQ and routes which produce and consume the message to from ActiveMQ message broker. I will also show how to send/receive to/from queue and topic.
Below are the beans which manage the transactions to exchange the messages between camel and ActiveMQ,
<bean id="activeMQConFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
<property name="userName" value="userName_activemq"></property>
<property name="password" value="password_activemq"></property>
</bean>
<bean id="pooledConFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
init-method="start" destroy-method="stop">
<property name="maxConnections" value="5" />
<property name="connectionFactory" ref="activeMQConFactory" />
</bean>
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConFactory" />
<property name="concurrentConsumers" value="5" />
</bean>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig" />
</bean>
JMS Queue: Produce and consume messages using JMS Queue. Add below routes in your camel context file.
<!-- Route which sends message to the jms-queue with name test-queue -->
<camel:route id="MessageSenderToJMSQueue">
<camel:from uri="direct:SomeTestRoute" />
<camel:log logName="MessageSenderToJMSQueue" loggingLevel="INFO"
message="Entered in MessageSenderToJMSQueue : ${body}" />
<camel:removeHeaders pattern="*" />
<camel:to uri="activemq:queue:test-queue" />
</camel:route>
<!-- Route which receive message from the test-queue with name queue-name -->
<camel:route id="MessageReceiverFromJMSQueue">
<camel:from uri="activemq:queue:test-queue" />
<camel:log logName="MessageReceiverFromJMSQueue" loggingLevel="INFO"
message="Entered in MessageReceiverFromJMSQueue : ${body}" />
<camel:to uri="direct:SomeTestRoute" />
</camel:route>
JMS Topic: Produce and consume messages using JMS Topic. Add below routes in your camel context file.
<!-- Route which sends message to the jms-topic with name test-topic -->
<camel:route id="MessageSenderToJMSTopic">
<camel:from uri="direct:SomeTestRoute" />
<camel:log logName="MessageSenderToJMSTopic" loggingLevel="INFO"
message="Entered in MessageSenderToJMSTopic : ${body}" />
<camel:removeHeaders pattern="*" />
<camel:to uri="activemq:queue:test-topic" />
</camel:route>
<!-- Route which receive message from the jms-topic with name test-topic -->
<camel:route id="MessageReceiverFromJMSTopic">
<camel:from uri="activemq:topic:test-topic" />
<camel:log logName="MessageReceiverFromJMSTopic" loggingLevel="INFO"
message="Entered in MessageReceiverFromJMSTopic : ${body}" />
<camel:to uri="direct:SomeTestRoute" />
</camel:route>
That's it. Now, build your application and run on Fuse Server. Your apps is ready to produce and consume messages from ActiveMQ using JMS API.
I hope you have enjoyed this camel riding. Please don't forget to give your feedback and suggestion to improve this article and of course my blog writing.
Thanks a lot for your time to read this blog.
No comments:
Post a Comment