This is a quickstart standalone demonstration of JMS API. We will use Apache ActiveMQ as the provider implementation of JMS and will show point-to-point messaging model in this example.
In this demo, the usage of ActiveMQ API is limited to only one class; JmsProvider, which creates an instance of ActiveMQConnectionFactory (an implementation of javax.jms.ConnectionFactory). The rest of the classes show the use of JMS API only.
ActiveMQ maven dependency
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.3</version>
</dependency>
Message producer
package com.logicbig.example;
import javax.jms.*;
public class ExampleMessageSender {
private final MessageProducer producer;
private final Session session;
private final Connection con;
public ExampleMessageSender () throws JMSException {
ConnectionFactory factory = JmsProvider.getConnectionFactory();
this.con = factory.createConnection();
con.start();
this.session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("example.queue");
this.producer = session.createProducer(queue);
}
public void sendMessage (String message) throws JMSException {
System.out.printf("Sending message: %s, Thread:%s%n",
message,
Thread.currentThread().getName());
TextMessage textMessage = session.createTextMessage(message);
producer.send(textMessage);
}
public void destroy () throws JMSException {
con.close();
}
}
Message consumer
package com.logicbig.example;
import javax.jms.*;
public class ExampleMessageReceiver implements MessageListener {
private Connection con;
public void startListener () throws JMSException {
ConnectionFactory factory = JmsProvider.getConnectionFactory();
this.con = factory.createConnection();
con.start();
Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("example.queue");
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(this);
}
@Override
public void onMessage (Message message) {
if (message instanceof TextMessage) {
TextMessage tm = (TextMessage) message;
try {
System.out.printf("Message received: %s, Thread: %s%n",
tm.getText(),
Thread.currentThread().getName());
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}
public void destroy () throws JMSException {
con.close();
}
}
JmsProvider.java
package com.logicbig.example;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.ConnectionFactory;
public class JmsProvider {
public static ConnectionFactory getConnectionFactory () {
/*The VM transport allows clients to connect to each other inside
the VM without the overhead of the network communication. */
ConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("vm://localhost");
return connectionFactory;
}
}
The main class
package com.logicbig.example;
public class Main {
public static void main (String[] args) throws Exception {
final ExampleMessageSender sender = new ExampleMessageSender();
final ExampleMessageReceiver receiver = new ExampleMessageReceiver();
receiver.startListener();
for (int i = 1; i <= 5; i++) {
String m = "Hello world! " + i;
sender.sendMessage(m);
Thread.sleep(300);
}
sender.destroy();
receiver.destroy();
}
}
Output
INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\examples\activemq-data\localhost\KahaDB]
INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
INFO | KahaDB is version 6
INFO | PListStore:[D:\examples\activemq-data\localhost\tmp_storage] started
INFO | Apache ActiveMQ 5.14.3 (localhost, ID:xxxxxxxx) is starting
INFO | Apache ActiveMQ 5.14.3 (localhost, ID:xxxxxxxx) started
INFO | For help or more information please see: http://activemq.apache.org
WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: D:\examples\activemq-data\localhost\KahaDB only has 75239 mb of usable space. - resetting to maximum available disk space: 75239 mb
INFO | Connector vm://localhost started
Sending message: Hello world! 1, Thread:main
Message received: Hello world! 1, Thread: ActiveMQ Session Task-1
Sending message: Hello world! 2, Thread:main
Message received: Hello world! 2, Thread: ActiveMQ Session Task-1
Sending message: Hello world! 3, Thread:main
Message received: Hello world! 3, Thread: ActiveMQ Session Task-1
Sending message: Hello world! 4, Thread:main
Message received: Hello world! 4, Thread: ActiveMQ Session Task-1
Sending message: Hello world! 5, Thread:main
Message received: Hello world! 5, Thread: ActiveMQ Session Task-1
INFO | Connector vm://localhost stopped
INFO | Apache ActiveMQ 5.14.3 (localhost, ID:xxxxxxxx) is shutting down
INFO | PListStore:[D:\examples\activemq-data\localhost\tmp_storage] stopped
INFO | Stopping async queue tasks
INFO | Stopping async topic tasks
INFO | Stopped KahaDB
INFO | Apache ActiveMQ 5.14.3 (localhost, ID:xxxxxxxx) uptime 2.434 seconds
INFO | Apache ActiveMQ 5.14.3 (localhost, ID:xxxxxxxx) is shutdown
Example project
Dependencies and Technologies Used: - activemq-all 5.14.3: Puts together an ActiveMQ jar bundle.
- JDK 1.8
- Maven 3.3.9
|