Polling TCP client connector

Right now TCP inbound endpoints are implemented as TCP servers that listen for data coming from different clients. In Mule ESB 2.2.6 we are adding a new feature to inverse the control: TCP inbounds can now poll data from remote servers.
It is really easy to switch to this strategy. Let’s take a look of how a mule configuration looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tcp="http://www.mulesource.org/schema/mule/tcp/2.2" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd http://www.mulesource.org/schema/mule/tcp/2.2 http://www.mulesource.org/schema/mule/tcp/2.2/mule-tcp.xsd"> <vm:connector name="queue" queueEvents="true" /> <tcp:polling-connector name="pollingConnector" clientSoTimeout="3000" pollingFrequency="1000"> <tcp:direct-protocol payloadOnly="true" /> </tcp:polling-connector> <model name="echoModel"> <service name="echo"> <inbound> <tcp:inbound-endpoint host="localhost" port="4444" /> </inbound> <outbound> <pass-through-router> <vm:outbound-endpoint path="out" connector-ref="queue" /> </pass-through-router> </outbound> </service> </model> </mule> |
Notice the new tcp:polling-connector element in the mule configuration file. This element tells mule to poll data with a frequency of 1000ms and a timeout of 3000ms.
Using the above configuration we can create a TCP server that writes messages like this:
1 2 3 4 5 6 7 | String stringMessage = "the message"; ServerSocket serverSocket = new ServerSocket(4444); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); out.print(stringMessage); out.close(); clientSocket.close(); |
You can check the value received in the out outbound using the MuleClient like this:
1 2 3 | MuleClient client = new MuleClient(); MuleMessage message = client.request("vm://out?connector=queue", 10000); String receivedMessage = message.getPayloadAsString(); |
The receivedMessage variable will hold the received value from the TCP Server.
If you want to try it, please download Mule ESB 2.2.6 or the latest 3.0 milestone