Вопрос

I'm trying to build a MQ producer that sends a message to an ActiveMQ queue. The thing is that I don't want the implementation to be specific for ActiveMQ. I actually am doing the ActiveMQ sending in dev environment, and in production the application server will use something else. I'm planning to use Maven to create 2 profiles that will filter resources based on the given profile.

I started playing with JNDI, but I'm stuck... I tried a lot of options, but none are viable. For now, my spring config xml is like this:

<jee:jndi-lookup id="mqConnectionFactory" jndi-name="java:comp/env/jms/mqConnectionFactory" />

<bean id="jmsJndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</prop>
            <prop key="java.naming.provider.url">vm://localhost</prop>
        </props>
    </property>
</bean>

<bean id="ismeJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="defaultDestination" ref="ismeJmsDestination"/>
    <property name="connectionFactory" ref="mqConnectionFactory"/>
</bean>

<bean id="ismeJmsDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate" ref="jmsJndiTemplate"/>
    <property name="jndiName" value="dynamicQueues/FOO.BAR"/>
</bean>

mqConnectionFactory cannot be found because I didn't add it in JNDI. I don't know how to add it actually, as this is not a webapp, so no context.xml.

Can someone help me in the right direction?

Это было полезно?

Решение

Whatever broker implementation you chose, you need to have the corresponding client library in your classpath. The JMS API is just an API, you need an actual implementation to connect to the broker of your choice.

Say you want to connect to ActiveMQ in development and to whatever JMS implementation in production. You can isolate those two config in separate profiles and make the activeMQ dependency an optional dependency in Maven.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top