« Return to Thread: Want to write a standalone java class that uses MuleClient for sending messages to ActiveMQ

Re: Want to write a standalone java class that uses MuleClient for sending messages to ActiveMQ

by Manupriya :: Rate this Message:

Reply to Author | View in Thread

Hi,

My requirement is to have a java class that creates and puts JMS messages to an ActiveMQ topic.
At present I am using JNDI lookup for the topic and then I am creating a JMX producer for this topic. After this I create a JMS message and putting it on the topic.

I read through some of the articles on net that I can use Mule Client for achieving the same. I am quite new to Mule, Can you please suggest which would be a better design approach - using JMS producer or Mule Client?

Thanks,
Manu



If you must use MuleClient, then you can try this:

public void sendMessage(Object msg){
        Map props = new HashMap();
        props.put("Sender", "Mule Client");
        MuleClient muleClient = new MuleClient();
        muleClient.dispatch("jms://localhost:61616/topic:manu.topic", msg, props);
}
The only problem with this is that your Java class needs to have the information about Active MQ, topic name, etc.

But if you want to send a message through Mule but don't have to use MuleClient, I would recommend this approach to remove all transport specific information from your Java client:

Add this to your mule config:
<jms:activemq-connector name="JMSConnector" connectionFactory-ref="activeMqConnectionFactory">
<spring:beans>
  <spring:bean name="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <spring:property name="brokerURL" value="tcp://localhost:61616"/>
  </spring:bean>
</spring:beans>
</jms:activemq-connector>
<service name="TopicService">
        <outbound>
            <outbound-pass-through-router>
                 <jms:outbound-endpoint queue="topic:manu.topic" transformer-refs="ObjectToJMSTransformer" />
            </outbound-pass-through-router>
        </outbound>
</service>


And in your Java class, you can send your message using the service name "TopicService":

Service service = MuleServer.getMuleContext().getRegistry().lookupService("TopicService");




Manupriya wrote:
Hi,

My requirement is to have a standalone java class that just generates messages and puts them to an Active MQ Topic. I want to write something similar to following -

        try {
               MuleClient client = new MuleClient();

                RemoteDispatcher dispatcher = client.getRemoteDispatcher("tcp://localhost:61616");
               
                 for (String rowValue : rowList) {
                         dispatcher.sendAsyncRemote("jms://topic:manu.topic", rowValue, null);
                 }
                } catch (MuleException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

Can you please help?

Thanks,
Manupriya

 « Return to Thread: Want to write a standalone java class that uses MuleClient for sending messages to ActiveMQ