【发布时间】:2016-01-18 12:57:23
【问题描述】:
我对 Spring 和 JMS 非常陌生。我一直在尝试提出一个涉及activemq和Spring的实现,如下所示。
spring-context.xml
<bean id="sampleApacheConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" lazy-init="true">
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="userName" value=“kodeseeker"/>
<property name="password" value=“mypassword"/>
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="sampleApacheConnectionFactory" />
</bean>
<!-- Default Destination Queue Definition-->
<bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="test.Foo"/>
</bean>
<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="defaultDestination" />
</bean>
<!-- Message Sender Definition -->
<bean id="messageSender" class="com.mypackage.Publisher2">
</bean>
<!-- Message Receiver Definition -->
<bean id="messageReceiver" class="com.mypackage.Listener">
</bean>
<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="test.Foo" />
<property name="messageListener" ref="messageReceiver" />
</bean>
</beans>
Publisher2.java
public class Publisher2 {
@Autowired
protected JmsTemplate jmsTemplate;
.......
// function called to perform update.
public void publishUpdate(final CustomMessage payload) throws JMSException {
LOGGER.entry();
try {
JmsTemplate jmsTemp= this.jmsTemplate;
if(jmsTemp ==null){
//jmsTemplate is ALWAYS null.
LOGGER.error("Jms Template is never initialized!!");
return;
}
jmsTemp.send(new MessageCreator(){
@Override
public Message createMessage(Session session) throws JMSException {
Message message = message(payload,session);
LOGGER.info("Sending message");
return message;
}
});
} catch (Exception jmsExcpetion) {
LOGGER.error("Error placing message on Queue",jmsExcpetion);
}
LOGGER.exit();
}
}
为了初始化jmsTemplate,我有什么特别需要做的吗?如有必要,我很乐意提供更多详细信息。
编辑 1:
类调用publishupdate
public class UpdateHandlerImpl implements UpdateHandler {
private final Publisher2 publisher;
....
public UpdateHandlerImpl() {
this(new Publisher2());
}
public UpdateHandlerImpl(
final Publisher2 publisher) {
this. publisher = publisher;
}
....
@Override
public void handle(final CustomMessage entity) {
try {
publisher. publishUpdate(entity);
} catch (final JMSException e) {
LOGGER.error("Error sending message", e);
}
}
…..
}
编辑 3: 基于@keith 输入的UpdateHandlerImpl 更新版本
public class UpdateHandlerImpl implements UpdateHandler {
//Hoping spring wires this?
Publisher2 publisher;
@Override
public void handle(final CustomMessage entity) {
try {
publisher. publishUpdate(entity);
} catch (final JMSException e) {
LOGGER.error("Error sending message", e);
}
}
…..
}
编辑 2: spring 上下文在启动时使用以下注释通过 mule(这是一个 mule 应用程序)加载。
<spring:beans>
<spring:import resource="classpath:spring-context.xml" />
</spring:beans>
【问题讨论】:
-
显示你正在调用的课程
publishUpdate。 -
@chrylis 。添加了详细信息。我希望它有所有必需的信息,如果不能随意询问。我可以提供更多背景信息。
标签: java spring jms mule activemq