gpt4 book ai didi

java - 为什么 jmsTemplate 总是空的?使用 spring 和 Apache ActiveMQ

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:13 24 4
gpt4 key购买 nike

<分区>

我是 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>

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com