简体   繁体   中英

Mule using JMS Connector in transformer

I'm writing a transformer that, among other things, needs to set some content on the message based upon whether there are messages pending in a given queue in ActiveMQ.

Is there a way to pass the jms:activemq-connector into the transformer code and make calls against it in the transformMessage() method to get the queue count?

Edit:

I found a way to get to the connector using the MuleContext but I'm not seeing the results I expect; the queue always appears to be empty. I loaded up the queue and then ran a flow that invoked the transformer. In the beginning of that transformer I had the following:

public Object transformMessage( MuleMessage message, String outputEncoding )
        throws TransformerException
{
    MuleContext context = message.getMuleContext();
    MuleRegistry registry = context.getRegistry();
    JmsConnector connector = (JmsConnector)registry.lookupConnector( "AMQConnector" );

    try
    {
        Session session = connector.getSession( false, false );
        Queue queue = session.createQueue( "MyQueue" );
        QueueBrowser browser = session.createBrowser( queue );
        Enumeration enumeration = browser.getEnumeration();
        boolean hasMessages = enumeration.hasMoreElements();
        System.out.println( "Value is: " + hasMessages );
    }
    catch ( Exception e )
    {
    }
....

I see the expected number of messages coming out...ie, if I started with 8 messages in the queue, I see the message 8 times. However, the value of hasMessages is always false.

I guess my question has changed more to, "How do I get the count of a queue from within a Mule transformer?"

Mule doesn't have any OOTB support for queue browsing so your approach is correct.

Do you have an active inbound endpoint that is consuming the queue you're browsing in the transformer? If yes, maybe you're getting false for hasMessages simply because the ActiveMQ client has pre-fetched all the messages already so the queue browser doesn't see any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM