spring消息發送的核心架構是JmsTemplate,隔離了像打開,關閉Session和Producer的繁瑣操作,因此應用開發人員僅僅需要關注實際的業務邏輯。
不建議使用JmsTemplate的receive()調用,因爲在JmsTemplate上的所有調用都是同步的,這意味着調用線程需要被阻塞,直到方法返回,這對性能影響很大。
建議使用監聽器來做消息的接收,這個是異步的。
具體是使用DefalutMessageListenerContainer,它允許異步接收消息並緩存session和消息consumer,而且還可以根據消息數量動態的增加或者縮減監聽器的數量。
增加maven依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-pool -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
</dependency>
搞一下spring配置
<!--工廠類,jms規範第一步-->
<bean id = "jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class = "org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://hadoop2:61616</value>
</property>
</bean>
</property>
<!--連接池的最大連接數-->
<property name="maxConnections" value="100"></property>
</bean>
<!--sping默認自己搞了個queue,後面的配置會用到-->
<bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-queue"></constructor-arg>
</bean>
<!--jmsTemplate-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="defaultDestination" />
<property name="messageConverter" >
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean>
</property>
</bean>
寫個producer
@Component("producerSpring")
public class ProducerSpring {
@Resource
JmsTemplate jmsTemplate;
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
ProducerSpring producer = (ProducerSpring)context.getBean("producerSpring");
producer.jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText("hello spring-jms");
return textMessage;
}
});
}
}
寫個consumer
@Component("consumerSpring")
public class ConsumerSpring {
@Resource
private JmsTemplate jmsTemplate;
public static void main(String[] args) throws JMSException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
ConsumerSpring consumer = (ConsumerSpring) context.getBean("consumerSpring");
while (true) {
//consumer.jmsTemplate.setReceiveTimeout(500);
Message message = consumer.jmsTemplate.receive("spring-queue");
if (message == null) break;
TextMessage textMessage = (TextMessage) message;
System.out.println("===" + textMessage.getText());
}
}
}
一般來說,不用直接寫consumer,這個是同步的。要用異步的listener更好,代碼以後補上。