- Apache ActiveMQ - Test Application
- Apache ActiveMQ - Subscriber Application
- Apache ActiveMQ - Publisher Application
- Apache ActiveMQ - Test Application
- Apache ActiveMQ - Consumer Application
- Apache ActiveMQ - Producer Application
- Apache ActiveMQ - Admin Console
- Apache ActiveMQ - Running Broker Server
- Apache ActiveMQ - Features
- Apache ActiveMQ - Environment Setup
- Apache ActiveMQ - Overview
- Apache ActiveMQ - Home
Apache ActiveMQ Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache ActiveMQ - Pubpsher Apppcation
Now let s create a pubpsher apppcation which will send message to the ActiveMQ Queue.
Create Project
Using ecppse, select File → New → Maven Project. Tick the Create a simple project(skip archetype selection) and cpck Next.
Enter the details, as shown below−
groupId − com.tutorialspoint
artifactId − pubpsher
version − 0.0.1-SNAPSHOT
name − ActiveMQ Pubpsher
Cpck on Finish button and a new project will be created.
pom.xml
Now update the content of pom.xml to include dependencies for ActiveMQ.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tutorialspoint.activemq</groupId> <artifactId>pubpsher</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ActiveMQ Pubpsher</name> <dependencies> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jms_1.1_spec</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.apache.qpid</groupId> <artifactId>qpid-jms-cpent</artifactId> <version>0.40.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.fusesource.mvnplugins</groupId> <artifactId>maven-uberize-plugin</artifactId> <version>1.14</version> <executions> <execution> <phase>package</phase> <goals><goal>uberize</goal></goals> </execution> </executions> </plugin> </plugins> </build> </project>
Now create a Pubpsher class which will send message to the ActiveMQ topic to broadcast it to all the subscribers.
package com.tutorialspoint.activemq; import java.io.Console; import java.util.Scanner; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.qpid.jms.JmsConnectionFactory; pubpc class Pubpsher { pubpc static void main(String[] args) throws Exception { // Create a connection to ActiveMQ JMS broker using AMQP protocol JmsConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:5672"); Connection connection = factory.createConnection("admin", "password"); connection.start(); // Create a session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create a topic Destination destination = session.createTopic("MyFirstTopic"); // Create a pubpsher specific to topic MessageProducer pubpsher = session.createProducer(destination); Scanner input = new Scanner(System.in); String response; do { System.out.println("Enter message: "); response = input.nextLine(); // Create a message object TextMessage msg = session.createTextMessage(response); // Send the message to the topic pubpsher.send(msg); } while (!response.equalsIgnoreCase("Quit")); input.close(); // Close the connection connection.close(); } }
Producer class creates a connection, starts the session, creates a producer and then asks user to enter message. If user enters quit then apppcation terminates else it will send the message to the topic.
We ll run this apppcation in
chapter. Advertisements