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
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();
}
}
Is there anything I specifically need to do in order to initialize jmsTemplate
? Id be happy to provide any more details if necessary .
Edit 1:
Class calling the 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);
}
}
…..
}
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);
}
}
…..
}
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>