From 7bd681339e322b0b776684211fb1a26c70760ab3 Mon Sep 17 00:00:00 2001 From: Austin Geannopoulos Date: Mon, 8 May 2017 13:35:08 -0400 Subject: [PATCH 1/3] Added two new example classes, Client and Server --- .../org/apache/qpid/jms/example/Client.java | 204 ++++++++++++++++++ .../org/apache/qpid/jms/example/Server.java | 119 ++++++++++ 2 files changed, 323 insertions(+) create mode 100644 qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java create mode 100644 qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java diff --git a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java new file mode 100644 index 000000000..b1c35ee55 --- /dev/null +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java @@ -0,0 +1,204 @@ + +package org.apache.qpid.jms.example; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.DeliveryMode; +import javax.jms.Destination; +import javax.jms.ExceptionListener; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TemporaryQueue; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Scanner; + +public class Client { + private static final int DELIVERY_MODE = DeliveryMode.NON_PERSISTENT; + public static void main(String[] args) throws Exception { + try { + // The configuration for the Qpid InitialContextFactory has been supplied in + // a jndi.properties file in the classpath, which results in it being picked + // up automatically by the InitialContext constructor. + Context context = new InitialContext(); + + ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup"); + Destination queue = (Destination) context.lookup("myQueueLookup"); + + Connection connection = factory.createConnection(System.getProperty("USER"), System.getProperty("PASSWORD")); + connection.setExceptionListener(new MyExceptionListener()); + connection.start(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + //Create message and temporary queue to send to and from. + Message messageToBeSent = userInterface(session); + TemporaryQueue tempQ = session.createTemporaryQueue(); + messageToBeSent.setJMSReplyTo(tempQ); + + MessageProducer messageProducer = session.createProducer(queue); + + //Send the message + messageProducer.send(messageToBeSent, DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + System.out.println("[CLIENT] The message has been sent."); + + + MessageConsumer messageConsumer = session.createConsumer(tempQ); + long start = System.currentTimeMillis(); + boolean deductTimeout = false; + int timeout = 1000; + + //Receive the server response + TextMessage newMessage = (TextMessage) messageConsumer.receive(timeout); + if (newMessage != null) { + System.out.print("[CLIENT] Response from server received in "); + } else { + System.out.println("[CLIENT] Response not received within timeout, stopping."); + deductTimeout = true; + } + + long finish = System.currentTimeMillis(); + long taken = finish - start; + if (deductTimeout) { + taken -= timeout; + } + if (newMessage != null) { + System.out.println(taken + "ms"); + } + + //Display response and close client. + System.out.println("[CLIENT] Here is the interpreted message:\n" + newMessage.getText() + "\n[CLIENT] Quitting Client."); + connection.close(); + System.exit(1); + + } catch (Exception exp) { + System.out.println("[CLIENT] Caught exception, exiting."); + exp.printStackTrace(System.out); + System.exit(1); + } + } + + //UI to generate message + private static Message userInterface(Session session) throws Exception { + System.out.print("From this client class, you can specify what you want the server to do.\n" + + "Choose an option:\n" + + "(1) Capitalize an inputted text message\n" + + "(2) Sort an inputted array of integers\n" + + "(3) Sort a randomly generated array of integers\n" + + "Your input: "); + Scanner key = new Scanner(System.in); + String userResponse = key.nextLine(); + + //Errorcheck user response + while (!userResponse.equals("1") && !userResponse.equals("2") && !userResponse.equals("3")) { + System.out.print("Invalid option. Repeat input: "); + userResponse = key.nextLine(); + } + + if (userResponse.equals("1")) { + return capitalize(key, session); + } else if (userResponse.equals("2")) { + return sort(key, session, false); + } else { + return sort(key, session, true); + } + } + + //Generates a text message to capitalize + private static Message capitalize(Scanner key, Session session) throws Exception { + System.out.print("Input the text message you want to be capitalized: "); + String userResponse = key.nextLine(); + + //Errorcheck user response + while (userResponse.trim().equals("")) { + System.out.print("Input can not be blank. Try again: "); + userResponse = key.nextLine(); + } + key.close(); + Message message = session.createTextMessage(userResponse); + message.setStringProperty("FUNCTION", "capitalize"); + return message; + } + + //Generates a list of integers to sort + private static Message sort(Scanner key, Session session, boolean isRandom) throws Exception { + ArrayList AL = new ArrayList(); + String userResponse; + + //If the list will be created by the user + if (!isRandom) { + int inputCount = 1; + System.out.println("Input each array element, or type \"done\" to stop."); + while (true) { + System.out.print("Input " + inputCount + ": "); + userResponse = key.nextLine(); + + //Errorcheck user response + if (isNumeric(userResponse)) { + AL.add(Integer.parseInt(userResponse)); + inputCount++; + } + else if (userResponse.equalsIgnoreCase("done")) { + if (inputCount == 1) { + System.out.println("There must be at least one number to send."); + } + else { + break; + } + } + else { + System.out.println("The input must either be a number input, or the terminating word \"done\"."); + } + } + + //If the list will be randomly generated + } else { + System.out.print("How many random integers will be generated?\nYour input: "); + userResponse = key.nextLine(); + + //Errorcheck user response + while (true) { + if (isNumeric(userResponse)) { + if (Integer.parseInt(userResponse) > 0) { + break; + } + } + System.out.print("Input must be positive integer. Try again: "); + userResponse = key.nextLine(); + } + for (int i = 0; i < Integer.parseInt(userResponse); i++) { + AL.add((int) (100*Math.random())); + } + } + key.close(); + Message message = session.createObjectMessage((Serializable) AL); + message.setStringProperty("FUNCTION", "sort"); + return message; + } + + private static boolean isNumeric(String str) { + try { + Integer.parseInt(str); + } + catch(NumberFormatException nfe) { + return false; + } + return true; + } + + private static class MyExceptionListener implements ExceptionListener { + @Override + public void onException(JMSException exception) { + System.out.println("[CLIENT] Connection ExceptionListener fired, exiting."); + exception.printStackTrace(System.out); + System.exit(1); + } + } +} diff --git a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java new file mode 100644 index 000000000..0177d0d83 --- /dev/null +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java @@ -0,0 +1,119 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.jms.example; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.DeliveryMode; +import javax.jms.Destination; +import javax.jms.ExceptionListener; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.jms.ObjectMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import java.util.ArrayList; +import java.util.Collections; + +public class Server { + + public static void main(String[] args) throws Exception { + System.out.println("[SERVER] Awaiting Message..."); + + try { + // The configuration for the Qpid InitialContextFactory has been supplied in + // a jndi.properties file in the classpath, which results in it being picked + // up automatically by the InitialContext constructor. + Context context = new InitialContext(); + + ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup"); + Destination queue = (Destination) context.lookup("myQueueLookup"); + + Connection connection = factory.createConnection(System.getProperty("USER"), System.getProperty("PASSWORD")); + connection.setExceptionListener(new MyExceptionListener()); + connection.start(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + + MessageConsumer messageConsumer = session.createConsumer(queue); + while (true) { + + long start = System.currentTimeMillis(); + + //Receive a message + Message receivedMessage = messageConsumer.receive(0); + + long finish = System.currentTimeMillis(); + long taken = finish - start; + + System.out.println("[SERVER] Received the message in " + taken + "ms"); + + //Create new message to return to client + Destination replyDestination = receivedMessage.getJMSReplyTo(); + TextMessage newMessage = session.createTextMessage(interpretMessage(receivedMessage)); + MessageProducer messageProducer = session.createProducer(null); + newMessage.setJMSDestination(replyDestination); + messageProducer.send(replyDestination, newMessage, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + System.out.println("[SERVER] The message has been interpreted and sent back to the client."); + } + + + + } catch (Exception exp) { + System.out.println("[SERVER] Caught exception, exiting."); + exp.printStackTrace(System.out); + System.exit(1); + } + } + + //Interpret received message and create a new one + private static String interpretMessage(Message receivedMessage) throws Exception + { + //If the message will be capitalized + if (receivedMessage.getStringProperty("FUNCTION").equals("capitalize")) { + return ((TextMessage) receivedMessage).getText().toUpperCase(); + + //If the message will be sorted + } else if (receivedMessage.getStringProperty("FUNCTION").equals("sort")) { + ObjectMessage receivedObject = (ObjectMessage) receivedMessage; + if (receivedObject.getObject() instanceof ArrayList) { + ArrayList newList = (ArrayList) receivedObject.getObject(); + Collections.sort(newList); + return newList.toString(); + } + } + return null; + } + + private static class MyExceptionListener implements ExceptionListener { + @Override + public void onException(JMSException exception) { + System.out.println("[SERVER] Connection ExceptionListener fired, exiting."); + exception.printStackTrace(System.out); + System.exit(1); + } + } +} \ No newline at end of file From 2474c859a757b475fba6e83afec6b18402dd9d4e Mon Sep 17 00:00:00 2001 From: Austin Geannopoulos Date: Thu, 11 May 2017 11:00:20 -0400 Subject: [PATCH 2/3] Updated the client+server examples to be more straightforward and cleaned up code --- .../org/apache/qpid/jms/example/Client.java | 192 +++++------------- .../org/apache/qpid/jms/example/Server.java | 43 +--- 2 files changed, 56 insertions(+), 179 deletions(-) diff --git a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java index b1c35ee55..e6659aec4 100644 --- a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java @@ -1,4 +1,23 @@ - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ package org.apache.qpid.jms.example; import javax.jms.Connection; @@ -16,12 +35,9 @@ import javax.naming.Context; import javax.naming.InitialContext; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Scanner; - public class Client { private static final int DELIVERY_MODE = DeliveryMode.NON_PERSISTENT; + public static void main(String[] args) throws Exception { try { // The configuration for the Qpid InitialContextFactory has been supplied in @@ -37,162 +53,48 @@ public static void main(String[] args) throws Exception { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - //Create message and temporary queue to send to and from. - Message messageToBeSent = userInterface(session); - TemporaryQueue tempQ = session.createTemporaryQueue(); - messageToBeSent.setJMSReplyTo(tempQ); + //Creates a message and temporary queue to send to and from. + int random = (int) (Math.random()*3); + TextMessage messageToBeSent; + if (random == 0) { + messageToBeSent = session.createTextMessage("first example message"); + } else if (random == 1) { + messageToBeSent = session.createTextMessage("second example message"); + } else { + messageToBeSent = session.createTextMessage("third example message"); + } + + TemporaryQueue tempQueue = session.createTemporaryQueue(); + messageToBeSent.setJMSReplyTo(tempQueue); MessageProducer messageProducer = session.createProducer(queue); - + //Send the message messageProducer.send(messageToBeSent, DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); - System.out.println("[CLIENT] The message has been sent."); - - - MessageConsumer messageConsumer = session.createConsumer(tempQ); - long start = System.currentTimeMillis(); - boolean deductTimeout = false; - int timeout = 1000; - + System.out.println("[CLIENT] The message with text \"" + messageToBeSent.getText() +"\" has been sent."); + + MessageConsumer messageConsumer = session.createConsumer(tempQueue); + //Receive the server response - TextMessage newMessage = (TextMessage) messageConsumer.receive(timeout); - if (newMessage != null) { - System.out.print("[CLIENT] Response from server received in "); + TextMessage receivedMessage = (TextMessage) messageConsumer.receive(1000); + if (receivedMessage != null) { + System.out.println("[CLIENT] Response from server received."); } else { System.out.println("[CLIENT] Response not received within timeout, stopping."); - deductTimeout = true; } - - long finish = System.currentTimeMillis(); - long taken = finish - start; - if (deductTimeout) { - taken -= timeout; - } - if (newMessage != null) { - System.out.println(taken + "ms"); - } - + //Display response and close client. - System.out.println("[CLIENT] Here is the interpreted message:\n" + newMessage.getText() + "\n[CLIENT] Quitting Client."); + System.out.println("[CLIENT] Here is the interpreted message:\n" + receivedMessage.getText() + "\n[CLIENT] Quitting Client."); connection.close(); System.exit(1); - + } catch (Exception exp) { System.out.println("[CLIENT] Caught exception, exiting."); exp.printStackTrace(System.out); System.exit(1); } } - - //UI to generate message - private static Message userInterface(Session session) throws Exception { - System.out.print("From this client class, you can specify what you want the server to do.\n" - + "Choose an option:\n" - + "(1) Capitalize an inputted text message\n" - + "(2) Sort an inputted array of integers\n" - + "(3) Sort a randomly generated array of integers\n" - + "Your input: "); - Scanner key = new Scanner(System.in); - String userResponse = key.nextLine(); - - //Errorcheck user response - while (!userResponse.equals("1") && !userResponse.equals("2") && !userResponse.equals("3")) { - System.out.print("Invalid option. Repeat input: "); - userResponse = key.nextLine(); - } - - if (userResponse.equals("1")) { - return capitalize(key, session); - } else if (userResponse.equals("2")) { - return sort(key, session, false); - } else { - return sort(key, session, true); - } - } - - //Generates a text message to capitalize - private static Message capitalize(Scanner key, Session session) throws Exception { - System.out.print("Input the text message you want to be capitalized: "); - String userResponse = key.nextLine(); - - //Errorcheck user response - while (userResponse.trim().equals("")) { - System.out.print("Input can not be blank. Try again: "); - userResponse = key.nextLine(); - } - key.close(); - Message message = session.createTextMessage(userResponse); - message.setStringProperty("FUNCTION", "capitalize"); - return message; - } - - //Generates a list of integers to sort - private static Message sort(Scanner key, Session session, boolean isRandom) throws Exception { - ArrayList AL = new ArrayList(); - String userResponse; - - //If the list will be created by the user - if (!isRandom) { - int inputCount = 1; - System.out.println("Input each array element, or type \"done\" to stop."); - while (true) { - System.out.print("Input " + inputCount + ": "); - userResponse = key.nextLine(); - - //Errorcheck user response - if (isNumeric(userResponse)) { - AL.add(Integer.parseInt(userResponse)); - inputCount++; - } - else if (userResponse.equalsIgnoreCase("done")) { - if (inputCount == 1) { - System.out.println("There must be at least one number to send."); - } - else { - break; - } - } - else { - System.out.println("The input must either be a number input, or the terminating word \"done\"."); - } - } - - //If the list will be randomly generated - } else { - System.out.print("How many random integers will be generated?\nYour input: "); - userResponse = key.nextLine(); - - //Errorcheck user response - while (true) { - if (isNumeric(userResponse)) { - if (Integer.parseInt(userResponse) > 0) { - break; - } - } - System.out.print("Input must be positive integer. Try again: "); - userResponse = key.nextLine(); - } - for (int i = 0; i < Integer.parseInt(userResponse); i++) { - AL.add((int) (100*Math.random())); - } - } - key.close(); - Message message = session.createObjectMessage((Serializable) AL); - message.setStringProperty("FUNCTION", "sort"); - return message; - } - - private static boolean isNumeric(String str) { - try { - Integer.parseInt(str); - } - catch(NumberFormatException nfe) { - return false; - } - return true; - } - + private static class MyExceptionListener implements ExceptionListener { @Override public void onException(JMSException exception) { @@ -201,4 +103,4 @@ public void onException(JMSException exception) { System.exit(1); } } -} +} \ No newline at end of file diff --git a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java index 0177d0d83..2d37fc53e 100644 --- a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java @@ -38,10 +38,7 @@ import java.util.Collections; public class Server { - public static void main(String[] args) throws Exception { - System.out.println("[SERVER] Awaiting Message..."); - try { // The configuration for the Qpid InitialContextFactory has been supplied in // a jndi.properties file in the classpath, which results in it being picked @@ -56,32 +53,23 @@ public static void main(String[] args) throws Exception { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageConsumer messageConsumer = session.createConsumer(queue); - while (true) { + MessageProducer messageProducer = session.createProducer(null); - long start = System.currentTimeMillis(); + while (true) { + System.out.println("[SERVER] Awaiting Message..."); //Receive a message - Message receivedMessage = messageConsumer.receive(0); - - long finish = System.currentTimeMillis(); - long taken = finish - start; - - System.out.println("[SERVER] Received the message in " + taken + "ms"); + Message receivedMessage = messageConsumer.receive(); + System.out.println("[SERVER] Received the message."); //Create new message to return to client Destination replyDestination = receivedMessage.getJMSReplyTo(); - TextMessage newMessage = session.createTextMessage(interpretMessage(receivedMessage)); - MessageProducer messageProducer = session.createProducer(null); - newMessage.setJMSDestination(replyDestination); + TextMessage newMessage = session.createTextMessage(capitalize(receivedMessage)); messageProducer.send(replyDestination, newMessage, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); - System.out.println("[SERVER] The message has been interpreted and sent back to the client."); + System.out.println("[SERVER] Returned new message to client."); } - - - } catch (Exception exp) { System.out.println("[SERVER] Caught exception, exiting."); exp.printStackTrace(System.out); @@ -89,23 +77,10 @@ public static void main(String[] args) throws Exception { } } - //Interpret received message and create a new one - private static String interpretMessage(Message receivedMessage) throws Exception + //Generate capitalized text to send back to client. + private static String capitalize(Message receivedMessage) throws Exception { - //If the message will be capitalized - if (receivedMessage.getStringProperty("FUNCTION").equals("capitalize")) { return ((TextMessage) receivedMessage).getText().toUpperCase(); - - //If the message will be sorted - } else if (receivedMessage.getStringProperty("FUNCTION").equals("sort")) { - ObjectMessage receivedObject = (ObjectMessage) receivedMessage; - if (receivedObject.getObject() instanceof ArrayList) { - ArrayList newList = (ArrayList) receivedObject.getObject(); - Collections.sort(newList); - return newList.toString(); - } - } - return null; } private static class MyExceptionListener implements ExceptionListener { From bc08c957d6d8242343f40eb3f4ab6a95d2ce9fbc Mon Sep 17 00:00:00 2001 From: Austin Geannopoulos Date: Mon, 15 May 2017 11:16:35 -0400 Subject: [PATCH 3/3] Updated client+server classes 5/15/17 --- .../org/apache/qpid/jms/example/Client.java | 49 ++++++------ .../org/apache/qpid/jms/example/Server.java | 74 +++++++++---------- 2 files changed, 56 insertions(+), 67 deletions(-) diff --git a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java index e6659aec4..babc0eae0 100644 --- a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java @@ -54,39 +54,36 @@ public static void main(String[] args) throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - //Creates a message and temporary queue to send to and from. - int random = (int) (Math.random()*3); - TextMessage messageToBeSent; - if (random == 0) { - messageToBeSent = session.createTextMessage("first example message"); - } else if (random == 1) { - messageToBeSent = session.createTextMessage("second example message"); - } else { - messageToBeSent = session.createTextMessage("third example message"); - } - + //Create a temporary queue to receive from, producer, and consumer. TemporaryQueue tempQueue = session.createTemporaryQueue(); - messageToBeSent.setJMSReplyTo(tempQueue); MessageProducer messageProducer = session.createProducer(queue); + MessageConsumer messageConsumer = session.createConsumer(tempQueue); - //Send the message - messageProducer.send(messageToBeSent, DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); - System.out.println("[CLIENT] The message with text \"" + messageToBeSent.getText() +"\" has been sent."); + //Create and send four messages. + String[] messageTexts = new String[] { "Twas brillig, and the slithy toves", + "Did gire and gymble in the wabe.", + "All mimsy were the borogroves,", + "And the mome raths outgrabe." }; - MessageConsumer messageConsumer = session.createConsumer(tempQueue); + for (String text : messageTexts) { + TextMessage messageToBeSent = session.createTextMessage(text); + messageToBeSent.setJMSReplyTo(tempQueue); - //Receive the server response - TextMessage receivedMessage = (TextMessage) messageConsumer.receive(1000); - if (receivedMessage != null) { - System.out.println("[CLIENT] Response from server received."); - } else { - System.out.println("[CLIENT] Response not received within timeout, stopping."); + messageProducer.send(messageToBeSent, DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); } - //Display response and close client. - System.out.println("[CLIENT] Here is the interpreted message:\n" + receivedMessage.getText() + "\n[CLIENT] Quitting Client."); - connection.close(); - System.exit(1); + //Receive the messages. + for (int i = 1; i <= messageTexts.length; i++) { + TextMessage receivedMessage = (TextMessage) messageConsumer.receive(1000); + if (receivedMessage != null) { + System.out.println("[CLIENT] Received Message " + i + ": " + messageTexts[i-1] + " ---> " + receivedMessage.getText()); + } else { + System.out.println("[CLIENT] Message " + i + " was not received within the timeout."); + } + } + + System.out.println("[CLIENT] Exiting..."); + System.exit(0); } catch (Exception exp) { System.out.println("[CLIENT] Caught exception, exiting."); diff --git a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java index 2d37fc53e..bbb7bd46f 100644 --- a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java @@ -39,48 +39,40 @@ public class Server { public static void main(String[] args) throws Exception { - try { - // The configuration for the Qpid InitialContextFactory has been supplied in - // a jndi.properties file in the classpath, which results in it being picked - // up automatically by the InitialContext constructor. - Context context = new InitialContext(); - - ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup"); - Destination queue = (Destination) context.lookup("myQueueLookup"); - - Connection connection = factory.createConnection(System.getProperty("USER"), System.getProperty("PASSWORD")); - connection.setExceptionListener(new MyExceptionListener()); - connection.start(); - - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - MessageConsumer messageConsumer = session.createConsumer(queue); - MessageProducer messageProducer = session.createProducer(null); - - while (true) { - System.out.println("[SERVER] Awaiting Message..."); - - //Receive a message - Message receivedMessage = messageConsumer.receive(); - System.out.println("[SERVER] Received the message."); - - //Create new message to return to client - Destination replyDestination = receivedMessage.getJMSReplyTo(); - TextMessage newMessage = session.createTextMessage(capitalize(receivedMessage)); - messageProducer.send(replyDestination, newMessage, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); - System.out.println("[SERVER] Returned new message to client."); - } - } catch (Exception exp) { - System.out.println("[SERVER] Caught exception, exiting."); - exp.printStackTrace(System.out); - System.exit(1); + try { + // The configuration for the Qpid InitialContextFactory has been supplied in + // a jndi.properties file in the classpath, which results in it being picked + // up automatically by the InitialContext constructor. + Context context = new InitialContext(); + + ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup"); + Destination queue = (Destination) context.lookup("myQueueLookup"); + + Connection connection = factory.createConnection(System.getProperty("USER"), System.getProperty("PASSWORD")); + connection.setExceptionListener(new MyExceptionListener()); + connection.start(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageConsumer messageConsumer = session.createConsumer(queue); + MessageProducer messageProducer = session.createProducer(null); + + while (true) { + //Receive messages and return a new uppercase message. + TextMessage receivedMessage = (TextMessage) messageConsumer.receive(); + + System.out.println("[SERVER] Received: " + receivedMessage.getText()); + + TextMessage responseMessage = session.createTextMessage(receivedMessage.getText().toUpperCase()); + + messageProducer.send(receivedMessage.getJMSReplyTo(), responseMessage, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); } - } - - //Generate capitalized text to send back to client. - private static String capitalize(Message receivedMessage) throws Exception - { - return ((TextMessage) receivedMessage).getText().toUpperCase(); + + } catch (Exception exp) { + System.out.println("[SERVER] Caught exception, exiting."); + exp.printStackTrace(System.out); + System.exit(1); + } } private static class MyExceptionListener implements ExceptionListener {