- Messaging in Spring(JMS)
- Relating to asynchronous messaging
- Use ActiveMQ (with maven)
- create connection factory
<bean id="connectionFactory" <---- To send message through message broker.
class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL"value="tcp://localhost:61616"/>
</bean>
OR
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
...
<amq:connectionFactoryid="connectionFactory"
brokerURL="tcp://localhost:61616"/>
- Declaring an ActiveMQ message destination
Can be a queue
<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-argvalue="spitter.queue"/> <------ Specific the name of the queue
</bean>
or
<amq:queueid="queue"physicalName="spitter.queue"/>
or a topic
<bean id="topic"class="org.apache.activemq.command.ActiveMQTopic">
<constructor-argvalue="spitter.topic"/> <------ Specific the name of the topic
</bean>
or
<amq:topicid="topic"physicalName="spitter.topic"/>
- Using Spring JMS Template
JmsTemplace is the center of the Spring JMS engine. Avoid the verbose and repetitive JMS code.
It creates a connection, obtains a session, sends and receives messages.
Wiring a JMS template
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory"ref="connectionFactory"/> <--- Reference to the connectionFactory
</bean>
Sending Messages
- Create a interface with some information
public interface AlertService{
void sendSpittleAlert(Spittle spittle);
}
- Create a implementation that uses JmsTemplate to send a object to a message queue
public class AlertServiceImpl implements AlertService{
public void sendSpittleAlert(final Spittle spittle){
jmsTemplate.send( <--- Send Message
"spittle.alert.queue", <---Destination
new MessageCreator(){
public Message createMessage(Sessionsession) throws JMSException{
return session.createObjectMessage(spittle); <--- Create the message
}
}
);
}
@Autowired
JmsTemplate jmsTemplate; <---- Inject JMS template
}
- It is possible to define a default destination. Then it is not necessary to pass the destination when sending messages
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory"ref="connectionFactory"/>
<property name="defaultDestinationName" value="spittle.alert.queue"/>
</bean>
And just call
jmsTemplate.send(
new MessageCreator(){
...
}
);
- Consuming a Message
public SpittlegetAlert(){
try {
ObjectMessage receivedMessage= (ObjectMessage) jmsTemplate.receive(); <--- Receive the message
return(Spittle)receivedMessage.getObject(); <--- Get the object
} catch(JMSExceptionjmsException){
throw JmsUtils.convertJmsAccessException(jmsException);
}
}
- Message Driven Bean using POJO
@MessageDriven(mappedName="jms/spittle.alert.queue") <---- Name of queue
public class SpittleAlertHandler implements MessageListener{
@Resource
private MessageDrivenContext mdc;
public void onMessage(Message message){
...
}
}
- Using just POJO
public classSpittleAlertHandler{
public voidprocessSpittle(Spittlespittle){
// ...implementationgoeshere...
}
}
Declaration
<bean id="spittleHandler" class="com.habuma.spitter.alerts.SpittleAlertHandler"/>
<jms:listener-container connection-factory="connectionFactory">
<jms:listener destination="spitter.alert.queue" ref="spittleHandler"method="processSpittle"/>
</jms:listener-container>
Nenhum comentário:
Postar um comentário