3
votes

Create connection factory where broker url to JVM

<!--tcp://localhost:61616-->
<bean id="connectionFactoryActiveMQ" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="vm://localhost"/>
    <property name="useAsyncSend" value="true"/>
</bean>

Create single connection factory becose need one connection

<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory" ref="connectionFactoryActiveMQ"/>
</bean>

create topic destination because need implemented some classes

<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
    <property name="physicalName" value="TEST"/>
</bean>

Generate jmsTemplate object

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="defaultDestination" ref="destination"/>
    <property name="pubSubDomain" value="true"/>
</bean>



My classes who receive messages
<bean id="messageListener1" class="com.aimrposoft.jms.server.Server"/>
<bean id="messageListener2" class="com.aimrposoft.jms.server.Server1"/>
<bean id="messageListener3" class="com.aimrposoft.jms.server.Server2"/>

Generate message class
<bean id="producer" class="com.aimrposoft.jms.client.Producer"/>



<!--<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">-->
<!--<property name="connectionFactory" ref="connectionFactory"/>-->
<!--<property name="destination" ref="destination"/>-->
<!--<property name="messageListener" ref="messageListener1"/>-->
<!--</bean>-->


<jms:listener-container
        connection-factory="connectionFactory" destination-type="topic" acknowledge="transacted">
    <jms:listener destination="TEST" ref="messageListener1" method="onMessage" subscription="subscription"/>
    <jms:listener destination="TEST" ref="messageListener2" method="onMessage" subscription="subscription"/>
    <jms:listener destination="TEST" ref="messageListener3" method="onMessage" subscription="subscription"/>

</jms:listener-container>

When I am using vm://localhost message listener don't working correctly, but if I run activeMQ and change broker URL to tcp://localhost:61616, all work is fine.

2

2 Answers

1
votes

I think you are missing configuration to start up a embedded broker, can you try adding this to your configuration also:

<amq:broker id="activeMQBroker">
    <amq:transportConnectors>
        <amq:transportConnector uri="vm://localhost" />
    </amq:transportConnectors>
</amq:broker>

amq namespace prefix can be defined this way:

xmlns:amq="http://activemq.apache.org/schema/core"
0
votes

To further Biju's answer, I use queues not topics, here's my spring config that appears to work just fine (62999 is just a random available port number):

    <amq:broker useJmx="false" persistent="false">
    <amq:transportConnectors>
        <amq:transportConnector uri="tcp://localhost:62999" />
    </amq:transportConnectors>
</amq:broker>

<bean id="rawConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="vm://localEmbeddedBroker" />
</bean>

<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory" ref="rawConnectionFactory" />
    <property name="sessionCacheSize" value="30" />
    <property name="cacheProducers" value="true" />
    <property name="cacheConsumers" value="false" />
</bean>