Time for action – using the UDP gateway

Let us now run the sample application that demonstrates the usage of the UDP gateway:

  1. In JBoss Developer Studio, open the jboss-esb.xml file in Source mode.
  2. Add the following listener to the listeners list:
    <listeners>
      <jms-listener busidref="chapter6ESBChannel"
                    name="Chapter6ESBListener"/>
      <jms-listener busidref="chapter6GwChannel" is-gateway="true"
                    name="Chapter6GwListener"/>
      <udp-listener name="Chapter6UDPListener" host="localhost"
                    port="9999" is-gateway="true"/>
    </listeners>
  3. Append the highlighted action definition to the <actions> tag:
    <actions mep="RequestResponse">
      <action class="org.jboss.soa.esb.samples.chapter6.MyAction"
              name="MyAction"/>
      <action name="notificationAction"
              class="org.jboss.soa.esb.actions.Notifier">
        <property name="okMethod" value="notifyOK"/>
        <property name="notification-details">
          <NotificationList type="ok">
            <target class="NotifyTcp" >
              <destination URI="tcp://localhost:8899" />
            </target>
          </NotificationList>
        </property>
      </action>
    </actions>
  4. Click the Save button and the modified application should now be deployed in the server.
  5. Select the src folder and expand it till the SendUDPMessage.java file is displayed in the tree. Now click Run | Run As | Java Application.

    The console will display the output as:

    UDP Gateway says Hello!
    

What just happened?

We sent a message via the UDP gateway listener and we received the response via a TCP socket. Here is the listing of the client code for reference:

public static void main(String[] args) throws Exception {
    ResponseReceiver receiver = new ResponseReceiver();
    new Thread(receiver).start();

    DatagramSocket socket = new DatagramSocket();
    socket.setSoTimeout(3000);
    String msg = "Hello UDP Gateway!";
    DatagramPacket packet = new DatagramPacket(msg.getBytes(),msg.getBytes().length);
    InetAddress address = InetAddress.getByName("localhost");
    packet.setAddress(address);
    packet.setPort(9999);
    socket.send(packet);
    socket.close();
}

private static class ResponseReceiver implements Runnable {

    public void run() {
        try {
            ServerSocket receiveSocket = new ServerSocket(8899);
            Socket clientSocket = receiveSocket.accept();
            BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            System.out.println(reader.readLine());
            reader.close();
            clientSocket.close();
            receiveSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset