Mule从组件返回MessageCollection

时间:2013-09-03 22:17:41

标签: mule transformer

我很难从组件返回MuleMessage实例的集合。

Mule 3.3.1。

以下代码有效(即,在组件之后,带有记录器的foreach组件会按照我的预期转储出“abc”和“def”。)

public Object onCall( MuleEventContext eventContext ) throws Exception 
{
    MuleMessage message = eventContext.getMessage();

    MuleMessageCollection collection = new DefaultMessageCollection( message.getMuleContext() );

    String s1 = "abc";
    String s2 = "def";
    DefaultMuleMessage m1 = new DefaultMuleMessage( s1, message.getMuleContext() );
    DefaultMuleMessage m2 = new DefaultMuleMessage( s2, message.getMuleContext() );
    List<MuleMessage> list = new ArrayList<MuleMessage>();
    list.add( m1 );
    list.add( m2 );

    collection.addMessages( list );

    return collection;
}

但是,如果我用自己的一个代替字符串代替字符串,那么:

public Object onCall( MuleEventContext eventContext ) throws Exception 
{
    MuleMessage message = eventContext.getMessage();

    MuleMessageCollection collection = new DefaultMessageCollection( message.getMuleContext() );

    LicenseRequest s1 = new LicenseRequest();
    LicenseRequest s2 = new LicenseRequest();
    DefaultMuleMessage m1 = new DefaultMuleMessage( s1, message.getMuleContext() );
    DefaultMuleMessage m2 = new DefaultMuleMessage( s2, message.getMuleContext() );
    List<MuleMessage> list = new ArrayList<MuleMessage>();
    list.add( m1 );
    list.add( m2 );

    collection.addMessages( list );

    return collection;
}

我得到一个例外:

org.mule.transport.http.HttpsConnector Work caused exception on 'workCompleted'. Work being executed was: org.mule.transport.http.HttpsMessageReceiver$HttpsWorker@7b921c57
org.mule.exception.DefaultSystemExceptionStrategy Caught exception in Exception Strategy: Payload was invalidated calling setPayload and the message is not collection anymore.
java.lang.IllegalStateException: Payload was invalidated calling setPayload and the message is not collection anymore.
    at org.mule.DefaultMessageCollection.checkValidPayload(DefaultMessageCollection.java:107)
    at org.mule.DefaultMessageCollection.newThreadCopy(DefaultMessageCollection.java:312)
    at org.mule.DefaultMuleEvent.newThreadCopy(DefaultMuleEvent.java:779)
    at org.mule.RequestContext.newEvent(RequestContext.java:140)
    at org.mule.RequestContext.setExceptionPayload(RequestContext.java:121)
    at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:54)
    at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:77)
    at org.mule.transport.http.HttpMessageReceiver$HttpWorker.run(HttpMessageReceiver.java:220)
    at org.mule.work.WorkerContext.run(WorkerContext.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Exception in thread "[license-generation].HTTPSConnector.receiver.02" java.lang.IllegalStateException: Payload was invalidated calling setPayload and the message is not collection anymore.
    at org.mule.DefaultMessageCollection.checkValidPayload(DefaultMessageCollection.java:107)
    at org.mule.DefaultMessageCollection.newThreadCopy(DefaultMessageCollection.java:312)
    at org.mule.DefaultMuleEvent.newThreadCopy(DefaultMuleEvent.java:779)
    at org.mule.RequestContext.newEvent(RequestContext.java:140)
    at org.mule.RequestContext.setExceptionPayload(RequestContext.java:121)
    at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:54)
    at org.mule.exception.AbstractSystemExceptionStrategy.handleException(AbstractSystemExceptionStrategy.java:77)
    at org.mule.transport.AbstractConnector.handleWorkException(AbstractConnector.java:2099)
    at org.mule.transport.AbstractConnector.workCompleted(AbstractConnector.java:2067)
    at org.mule.work.WorkerContext.run(WorkerContext.java:338)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

在我的测试中,LicenseRequest只是一个空类,其toString()方法返回“ghi”。

我做错了什么?

我应该补充一点,我的目标是返回从传入的有效负载创建的对象集合。然后,我可以在调用流中迭代这些对象,并为每个对象采取适当的操作。

编辑:看来我可以在变压器中做我想做的事情,而不是一个组件。这是为什么?

1 个答案:

答案 0 :(得分:1)

我想不需要将每个LicenseRequest对象包装成mule消息并将其添加到集合中。直接创建一个集合并从onCall方法返回它。

public Object onCall( MuleEventContext eventContext ) throws Exception 
{
    MuleMessage message = eventContext.getMessage();

    LicenseRequest s1 = new LicenseRequest();
    LicenseRequest s2 = new LicenseRequest();

    List<LicenseRequest> list = new ArrayList<LicenseRequest>();
    list.add( s1 );
    list.add( s2 );

    return list;
}

希望这有帮助。