Simultanous sending and receiving

2019-08-18 09:00发布

问题:

I am trying to send and receive message to and from JMS topics.

<testcase name="DeliveryToPT3PLIT">
        ...
        <actions>
            ...              
            <send endpoint="fromEndpoint">
                <message>
                    <resource file="com/roche/icc/citrus/messages/input/PT-3PLWoBatchSplit.xml"/>
                </message>
                <header>
                    ...
                </header>
            </send>

            <receive endpoint="toEndpoint">
                <description>Receive asynchronous message from topic</description>
                <message>
                        <resource file="com/roche/icc/citrus/messages/output/PT-3PLWoBatchSplit.xml"/>
                </message>
                <header>
                    ...
                </header>
            </receive>
        </actions>
    </testcase>

It seems that these operations go one after another. The problem is that my application works really fast and when I send a message to first topic it appears on "toEndpoint" almost immediately. So the receive operation does not manage to catch appropriate message since it has been already processed.

Is there any way to make this operations simultaneously?

Regards

回答1:

There is one thing to note about using JMS topics, because they work in a publish/subscribe manner, you have to subscribe first in order to receive the message.

That means you need to subscribe to the topic before a message is going to be published. Also, if you say that your application is fast, you may need to wait a few millis before sending the message. Here is a Java DSL example of what I think may work for you. (Note: I have tested this example with a JMS topic)

parallel().actions(
    sequential().actions( // Thread #1
            ...
            receive(action -> action.endpoint(toEndpoint).payload("message to be received"))),
    sequential().actions( // Thread #2
            sleep(500),
            send(action -> action.endpoint(fromEndpoint).payload("message to be sent"))));

Notice the parallel() action container. It will execute every action inside it in a separate thread. Each sequential() represents a series of actions that allows you to group them together. Because both sequential() containers are inside the parallel(), the actions inside each of them will be executed on different threads.

So what happens here: you split your actions on two threads. On the first thread you wait for a message. On the second thread, you make it sleep for 500 millis first, after which you send the message.

I'm sorry that I don't have an XML example, but I hope this helps anyway.

Also don't forget to set the pub-sub-domain property to true on your endpoints. See the Citrus documentation on JMS topics: here.