Let us now run the sample application that demonstrates the usage of the UDP gateway:
jboss-esb.xml
file in Source mode.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>
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>
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!
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(); } } }