为什么jmsTemplate总是为null?使用spring和Apache ActiveMQ [英] Why is jmsTemplate always null? Using spring and Apache ActiveMQ
问题描述
我对Spring和JMS还是很陌生.我一直在尝试提出一个涉及activemq和Spring的实现,如下所示.
Im very new to Spring and JMS. Ive been trying to come up with an implementation that involves activemq and Spring as follows.
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
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
,我需要专门做什么吗?如果有必要,我很乐意提供更多详细信息.
Is there anything I specifically need to do in order to initialize jmsTemplate
? Id be happy to provide any more details if necessary .
修改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的版本
Edit 3: Updated version of UpdateHandlerImpl based on @keith's input
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: 使用以下注释,在启动时通过mule(这是一个mule应用程序)加载spring上下文.
Edit 2: The spring context is loaded through mule( this is a mule application) on startup using the following annotation.
<spring:beans>
<spring:import resource="classpath:spring-context.xml" />
</spring:beans>
推荐答案
如果使用new
创建Publisher2
,则将不会在创建的实例上关联依赖项.而是在上下文文件中将其定义为Spring bean,然后从那里获取它.
If you are creating Publisher2
using new
, you will not have the dependencies wired on the instance you create. Instead, define it as a Spring bean in your context file and get it from there.
这是我所怀疑的,在您对问题的最新更新中,您确认您正在使用新内容创建Publisher2
.
It's as I suspected, in your latest update to the question you confirmed that you are creating a Publisher2
with new.
public UpdateHandlerImpl() {
this(new Publisher2());
}
这不是Spring的工作方式.参见第6.3.2节有关实例化bean的Spring Framework文档. (简而言之,使用上下文创建Bean)
This is not how Spring works. See Section 6.3.2 of the Spring Framework documentation on instantiating beans. (In short, create the bean using the context)
这篇关于为什么jmsTemplate总是为null?使用spring和Apache ActiveMQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!