пятница, 27 августа 2010 г.

Hornetq JMS integration with Tomcat. Spring configuration. Part 2 of 2

In second part i want to show spring configuration, that allow to use hornetq from spring beans via well know spring JmsTemplate.

    <bean name="mbeanServer" 
class="java.lang.management.ManagementFactory"
factory-method="getPlatformMBeanServer"/>

<bean name="hornetQSecurityManagerImpl"
class="org.hornetq.spi.core.security.HornetQSecurityManagerImpl"/>

<bean name="fileConfiguration"
class="org.hornetq.core.config.impl.FileConfiguration"
init-method="start"
destroy-method="stop"/>

<bean name="hornetQServerImpl"
class="org.hornetq.core.server.impl.HornetQServerImpl">
<constructor-arg ref="fileConfiguration"/>
<constructor-arg ref="mbeanServer"/>
<constructor-arg ref="hornetQSecurityManagerImpl"/>
</bean>

<bean name="jmsServerManagerImpl"
class="org.hornetq.jms.server.impl.JMSServerManagerImpl"
init-method="start"
destroy-method="stop">
<constructor-arg ref="hornetQServerImpl"/>
</bean>

<bean id="inVmConnectionFactory"
class="org.hornetq.jms.client.HornetQConnectionFactory">
<constructor-arg ref="inVmTransportConfiguration"/>
</bean>

<bean id="inVmTransportConfiguration"
class="org.hornetq.api.core.TransportConfiguration">
<constructor-arg index="0" value="org.hornetq.core.remoting.impl.invm.InVMConnectorFactory"/>
</bean>


    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.apache.naming.java.javaURLContextFactory</prop>
<prop key="java.naming.factory.url.pkgs">org.apache.naming</prop>
</props>
</property>
</bean>

<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"/>
<property name="jndiName" value="HQConnectionFactory"/>
</bean>


<bean id="customerRegistrationQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"/>
<property name="jndiName" value="queue/CustomerRegistrationQueue"/>
</bean>
<bean id="customerRegistrationJMSTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsQueueConnectionFactory"/>
<property name="defaultDestination" ref="customerRegistrationQueue"/>
</bean>



Configured JmsTemplate can be use in your spring bean. Actually i use jms notification in several system aspects. It allow to have fully decoupled system parts.

My base aspect class is following:
/** 
*
* Base class for notification aspects.
*
*/

public class BaseNotificationAspect {

private final JmsTemplate jmsTemplate;

/**
* Construct base notification aspect class.
* @param jmsTemplate jms teplate to use
*/

public BaseNotificationAspect(final JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

/**
* Send registration notification.
* @param serializableMessage object to send
*/

protected void sendNotification(final Serializable serializableMessage) {
if (jmsTemplate != null) {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(final Session session) throws JMSException {
return session.createObjectMessage(serializableMessage);
}
});
}
}
}


And derived class has following description as sprind bean:
    <bean id="customerRegistrationAspect" class="com.some.product.name.service.domain.aspect.impl.CustomerRegistrationAspect">
<constructor-arg index="0" ref="customerRegistrationJMSTemplate"/>
</bean>

Комментариев нет:

Отправить комментарий