Preparatory work:
ACTIVEMQ's message confirmation mechanism is that the ACK mechanism in the document is:
Auto_acknowledge = 1 Automatic Confirmation
Client_acknowledge = 2 Client Manual Confirmation
Dups_ok_acknowledge = 3 Automatic Batch Confirmation
session_transacted = 0 Transaction Commit and confirm
Individual_acknowledge = 4 Single Message confirmation ACTIVEMQ exclusive
The ACK pattern describes how the consumer and broker acknowledge the message (timing), such as when the message will be acknowledged by consumer when it is consumer received.
For the broker, only the ACK instruction is received, the message is considered to be properly received or processed successfully, by ACK, can be in consumer (/producer)
Establish a simple "guarantee" mechanism with broker.
Manual Confirmation and single message confirmation requires a manual call to Message.acknowledge () on the client
The message re-send mechanism Redeliverypolicy has several properties as follows:
Redeliverypolicy redeliverypolicy= new Redeliverypolicy ();
Whether to increase this wait time
Redeliverypolicy.setuseexponentialbackoff (true) after each attempt to resend failed;
The number of re-send, the default is 6 times here is set to 10 times
Redeliverypolicy.setmaximumredeliveries (ten);
The re-send interval, default is 1 seconds
redeliverypolicy.setinitialredeliverydelay (1);
Wait 500 milliseconds before resending for the first failure, and wait 500 * 2 milliseconds for the second failure, where 2 is value
redeliverypolicy.setbackoffmultiplier (2);
Whether to avoid message collision
Redeliverypolicy.setusecollisionavoidance (false);
Set the maximum delay time for the re-send-1 means no delay only Useexponentialbackoff (true) is true when
redeliverypolicy.setmaximumredeliverydelay (-1) is active;
The following conditions cause the message to be re-sent:
1. In the session using the transaction, call the rollback () method;
2. In the session using the transaction, the session is closed before the commit () method is called.
3. The Client_acknowledge signature mode or Individual_acknowledge mode is used in the session, and the Recover () method is called.
You can customize the re-routing policy you want by setting activemqconnectionfactory.
Note that using manual sign-on mode, if the client does not call the Message.acknowledge () method is not immediately resend the message, only the current Coustomer restart to accept the message
Spring Boot integrated ACTIVEMQ requires jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-activemq</artifactid>
</dependency>
Add your own configuration to the default configuration class for your own needs
Activemq4config as follows:
Package com.zyc.activemq;
Import Javax.jms.Queue;
Import Org.apache.activemq.ActiveMQConnectionFactory;
Import Org.apache.activemq.RedeliveryPolicy;
Import Org.apache.activemq.command.ActiveMQQueue;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.jms.annotation.EnableJms;
Import Org.springframework.jms.config.DefaultJmsListenerContainerFactory;
Import Org.springframework.jms.core.JmsTemplate; @EnableJms @Configuration public class Activemq4config {@Bean public queue queue () {return new Activemqqueue ("
Queue1 "); } @Bean Public Redeliverypolicy redeliverypolicy () {redeliverypolicy redeliverypolicy= new Redeliverypolicy
();
Whether to increase this wait time Redeliverypolicy.setuseexponentialbackoff (true) after each attempt to resend failed;
The number of re-send, the default is 6 times here set to 10 times redeliverypolicy.setmaximumredeliveries (10); //The re-send interval, default is 1 seconds redeliverypolicy.setinitialredeliverydelay (1);
Wait 500 milliseconds before resending for the first failure, and wait 500 * 2 milliseconds for the second failure, where 2 is value redeliverypolicy.setbackoffmultiplier (2);
Whether to avoid message collision redeliverypolicy.setusecollisionavoidance (false);
Set the maximum delay time for the re-send-1 means no delay only Useexponentialbackoff (true) is true when Redeliverypolicy.setmaximumredeliverydelay (-1) is active;
return redeliverypolicy; } @Bean Public Activemqconnectionfactory activemqconnectionfactory (@Value ("${activemq.url}") String Url,redelivery Policy redeliverypolicy) {activemqconnectionfactory activemqconnectionfactory = new Activemqco
Nnectionfactory ("admin", "admin", url);
Activemqconnectionfactory.setredeliverypolicy (Redeliverypolicy);
return activemqconnectionfactory; } @Bean Public jmstemplate jmstemplate (activemqconnectionfactory activemqconnectionfactory,qUeue queue) {jmstemplate jmstemplate=new jmstemplate (); Jmstemplate.setdeliverymode (2);//Persistent configuration 1 means non-persistent, 2 means persistent jmstemplate.setconnectionfactory (
Activemqconnectionfactory); Jmstemplate.setdefaultdestination (queue);
The default is not set here, the queue Jmstemplate.setsessionacknowledgemode (4) can also be set when sending a message;//client sign-in mode return jmstemplate; }//define a message listener connection factory, defined here is a peer-to-peer Mode Listener Connection Factory @Bean (name = "Jmsqueuelistener") public Defaultjmslistenercontainer Factory jmsqueuelistenercontainerfactory (activemqconnectionfactory activemqconnectionfactory) {DefaultJmsListener
Containerfactory factory = new Defaultjmslistenercontainerfactory ();
Factory.setconnectionfactory (activemqconnectionfactory);
Set the number of connections factory.setconcurrency ("1-10");
Re-connect interval factory.setrecoveryinterval (1000L);
Factory.setsessionacknowledgemode (4);
return factory; }
}
Consumers are as follows: Using asynchronous listening (using listener form)
Package Com.zyc.activemq.consumer;
Import javax.jms.JMSException;
Import javax.jms.Session;
Import Javax.jms.TextMessage;
Import Org.slf4j.Logger;
Import org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.jms.annotation.JmsListener;
Import org.springframework.stereotype.Component;
@Component public
class Consumer {
private final static Logger Logger = Loggerfactory
. GetLogger ( Consumer.class);
@JmsListener (Destination = "Queue1", containerfactory = "Jmsqueuelistener") public
void Receivequeue (final TextMessage text, session session)
throws JMSException {
try {
logger.debug ("Consumer received the message:" + Text.gettext ());
Text.acknowledge ();//use manual sign-off mode, require a manual call, if not in the catch call Session.recover () message will only be re-sent after restarting the service
} catch (Exception e) {
Session.recover ();//This must not omit the re-send message using
}}
}
The producers are as follows:
Package com.zyc.activemq.producer;
Import javax.jms.Destination;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.jms.annotation.JmsListener;
Import org.springframework.jms.core.JmsTemplate;
Import org.springframework.stereotype.Component;
@Component public
class Producer {
@Autowired
private jmstemplate jmstemplate;
/**
* Send Message, Estination is sent to the queue, message is to be sent messages
* @param destination
* @param
Message * * public void SendMessage (Destination Destination, final String message) {
System.out.println ( Jmstemplate.getdeliverymode ());
Jmstemplate.convertandsend (destination, message);
}
/**
* Send message, message is to be sent messages
* @param message
*
/public void SendMessage (final String message) {
System.out.println (Jmstemplate.getdeliverymode ());
Jmstemplate.convertandsend ("queue1", message);
}
}
The application.properties configuration file is as follows:
Spring.datasource.url=jdbc:mysql://localhost:3306/mydb
Spring.datasource.username=root
spring.datasource.password=123456
Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Activemq.url=failover: (tcp://127.0.0.1:61616)
The test is as follows: If you do not know springboot JUnit test can refer to springboot junit test
Package Com.zyc;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.boot.test.context.SpringBootTest;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import Org.springframework.test.context.junit4.SpringRunner;
Import Com.zyc.activemq.producer.Producer;
@RunWith (Springrunner.class)
@SpringBootTest (classes = app.class) public
class Applicationtest {
@ autowired
private Producer Producer;
@Test public
void Testactivemq () {
producer.sendmessage ("Look this is a message==zycc==");
while (true) {}
}
}
This article refers to the
http://shift-alt-ctrl.iteye.com/blog/2020182
http://blog.csdn.net/varyall/article/details/49907995