1. What is ActiveMQ?
ActiveMQ is an open source message middleware (Message Oriented Middleware, MOM) implemented by JMS Provider that fully supports JMS1.1 and J2EE1.4 specifications.
MOM's vernacular explanation: The projects we did at the beginning were all completed in one project, but later with business changes, we needed to extract the core functions as a separate project, which also reduced dependencies. This project may be called by multiple systems. At this time, we need a third-party message middleware for message delivery to decouple the systems.
ActiveMQ is the implementation of MOM. It is mainly used to reduce project dependencies and provide asynchronous invocation.
Second, ActiveMQ installation
1. Download the latest version of ActiveMQ from http://activemq.apache.org/activemq-5143-release.html
2. Unzip it directly, and then copy it to the location you want to install.
3. Go to ActiveMQ/bin directory./activemq start
or start and specify the log file ./activemq start >/tmp/activemqlog
4. Check if it has been started
ActiveMQ uses port 61616 to provide JMS service by default, and uses port 8161 to provide management console service. Execute the following command to verify whether ActiveMQ service has been successfully started.
a) Check if port 61616 is open: netstat -an | grep 61616
b) View console output or log files directly
c) Directly access ActiveMQ management page http://localhost:8161/admin/ default username and password admin/admin
d) To close ActiveMQ you can use ./activemq stop
e) Violent shutdown: ps -ef | grep activemq and then kill -9 port number to kill.
3. Basic use
1. Configure the dependencies required by Maven, examples are as follows:
pom configuration file
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.9.0</version> </dependency> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency>
sender:
public class JmsSend { public static void main(String[] args) { //create link factory ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); //create connection Connection connection = connectionFactory.createConnection(); // start the connection connection.start(); //create a session //Boolean.TRUE whether to use transaction //Session.AUTO_ACKNOWLEDGE message how to confirm Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //create destination //my-queue queue name takes its own name Destination destination = session.createQueue("my-queue"); //create the producer to send the message MessageProducer producer = session.createProducer(destination); for(int i=0; i<3; i++) { // encapsulate the specific message TextMessage message = session.createTextMessage("message--"+i); Thread.sleep(1000); // Send a message through the message producer producer.send(message); } // transaction commit session.commit(); //Close the session session.close(); //close the connection connection.close(); } }
Receiving end
public class JmsReceiver { public static void main(String[] args) throws Exception { ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = cf.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //create message destination //my-queue should be the same as the name sent Destination destination = session.createQueue("my-queue"); //create receiver MessageConsumer consumer = session.createConsumer(destination); int i=0; while(i<3) { i++; TextMessage message = (TextMessage) consumer.receive(); session.commit(); System.out.println("Received message: " + message.getText()); } session.close(); connection.close(); } }