生产流程图
消费流程图
1.生产端 pom.xml
<!-- active mq client start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.13.3</version>
</dependency>
2.生产端activemq.xml
<beans:bean id="jmsFactory"
class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<beans:property name="connectionFactory">
<beans:bean class="org.apache.activemq.ActiveMQConnectionFactory">
<beans:property name="brokerURL" value="${mq.url}" />
<beans:property name="trustAllPackages" value="true"></beans:property>
</beans:bean>
</beans:property>
<beans:property name="maxConnections" value="${mq.maxconnections}" />
</beans:bean>
<!--生产跟踪信息 -->
<!-- 定义消息队列(Queue)点对点队列 -->
<beans:bean id="trackQueue" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<beans:constructor-arg>
<beans:value>trackQueue</beans:value>
</beans:constructor-arg>
</beans:bean>
<!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
<beans:bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<beans:property name="connectionFactory" ref="jmsFactory" />
<beans:property name="defaultDestination" ref="trackQueue" />
<beans:property name="receiveTimeout" value="10000" />
<beans:property name="deliveryMode" value="1"/>
<!-- <beans:property name="alwaysSyncSend" value="true"/> -->
</beans:bean>
3.生产端代码 这个名称应该创建一个常量
jmsTemplate.send("trackQueue", new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(shipmentInfo.getTrackingNumber());
}
});
1.消费端pom.xml (上面的消费端是为配合老系统配置的 实际上生产端和消费端都只需要加入以下配置)
<!-- activemq -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.13.3</version>
</dependency>
2.生产端properties配置文件配置 mq.url=tcp://test.omniselling.net:61616?wireFormat.maxInactivityDuration=0
3.javaconfig 不推荐activemq.xml这种配置 这种配置没有提示 生产者是为了配合老系统而开发的 消费者这种配置 加入的jar包 才是我推荐的方式
@Configuration
public class ActiveConfig
{
@Value("${mq.url}")
public String activeUrl;
private static final String ORDER_QUEUE = "trackQueue";
@Autowired
ActiveTrackListener messageReceiver;
@Bean //不用创建连接池 用缓存连接更好
public ActiveMQConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(activeUrl);
connectionFactory.setTrustAllPackages(true);
//客户端异步转发
connectionFactory.setAlwaysSessionAsync(true);
return connectionFactory;
}
@Bean//CachingConnectionFactory类扩展自SingleConnectionFactory 高效
public ConnectionFactory sigleConnectionFactory(){
SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
connectionFactory.setTargetConnectionFactory(connectionFactory());
//缓存session100效率较高
return connectionFactory;
}
/*
* Message listener container, used for invoking messageReceiver.onMessage on message reception.
*/
@Bean
public MessageListenerContainer getContainer(){
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(sigleConnectionFactory());
container.setDestinationName(ORDER_QUEUE);
// singleconnectionfactory+cachesumer据说最能解决consumer阻塞问题
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
container.setMessageListener(messageReceiver);
container.setConcurrency("2");
return container;
}
4.消费监听器
@Component
public class ActiveTrackListener implements MessageListener
{
private Logger logger = LogManager.getLogger(ActiveTrackListener.class);
@Resource
private IBizShipmentTrackingProvider bizShipmentTrackingProvider;
@Override
public void onMessage(Message msg)
{
logger.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>当前执行时间: "+System.currentTimeMillis());
logger.info("Consumer收到的报文为:"+msg+"");
try
{
if(msg instanceof TextMessage) {
TextMessage txtMsg=(TextMessage)msg;
String message = txtMsg.getText();
msg.acknowledge();
List<BizShipmentTracking> tracks= bizShipmentTrackingProvider.getTrackingInformation(message, TmsConstant.SYSTEM_OPERATOR);
logger.info(message+">>"+tracks==null?"":tracks.size()>0?tracks.get(0).getTrackingInfo():"");
}
}
catch (JMSException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.error("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<当前执行时间: "+System.currentTimeMillis());
}
问题 如果一套应用在两台机器上部署 用的同一个activemq服务 并且开启了预存取 会有队列执行时间长或者不执行的情况.
源码地址:https://github.com/apache/activemq
案例地址:http://websystique.com/spring/spring-4-jms-activemq-example-with-annotations/