实践ActiveMQ之生产者 Posted on 2018-03-26 | In Java | Visitors 原生 12345678910111213141516171819202122232425ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection conn = null; Session session = null; try { conn = cf.createConnection(); session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = new ActiveMQQueue("queue001"); MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(); message.setText("error"); producer.send(message); } catch (JMSException e) { logger.error(e.getMessage(), e); } finally { try { if (session != null) { session.close(); } if (conn != null) { conn.close(); } } catch (JMSException ex) { logger.error(ex.getMessage(), ex); } } Spring JMS 12345<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 推荐使用CachingConnectionFactory --> <constructor-arg name="connectionFactory" ref="jmsConnectionFactory" /> <property name="defaultDestinationName" value="queue001" /> </bean> 123456jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage(message); return textMessage; } });