Repository: qpid-jms Updated Branches: refs/heads/master d3ce3dc38 -> 8a1a498c6
NO-JIRA: Added two new example classes, Client and Server Initial changes from PR #7 Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/72c7bb08 Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/72c7bb08 Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/72c7bb08 Branch: refs/heads/master Commit: 72c7bb0880b5cc7dd332c0220091f93d0de9a5a2 Parents: d3ce3dc Author: Austin Geannopoulos <[email protected]> Authored: Mon May 15 11:16:35 2017 -0400 Committer: Robert Gemmell <[email protected]> Committed: Wed May 17 12:12:09 2017 +0100 ---------------------------------------------------------------------- .../org/apache/qpid/jms/example/Client.java | 103 +++++++++++++++++++ .../org/apache/qpid/jms/example/Server.java | 86 ++++++++++++++++ 2 files changed, 189 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/72c7bb08/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.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 0000000..babc0ea --- /dev/null +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Client.java @@ -0,0 +1,103 @@ +/* + * + * 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.TemporaryQueue; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; + +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 a temporary queue to receive from, producer, and consumer. + TemporaryQueue tempQueue = session.createTemporaryQueue(); + MessageProducer messageProducer = session.createProducer(queue); + MessageConsumer messageConsumer = session.createConsumer(tempQueue); + + //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." }; + + for (String text : messageTexts) { + TextMessage messageToBeSent = session.createTextMessage(text); + messageToBeSent.setJMSReplyTo(tempQueue); + + messageProducer.send(messageToBeSent, DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + } + + //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."); + exp.printStackTrace(System.out); + System.exit(1); + } + } + + 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); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/72c7bb08/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/Server.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java new file mode 100644 index 0000000..bbb7bd4 --- /dev/null +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Server.java @@ -0,0 +1,86 @@ +/* + * + * 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 { + 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); + } + + } catch (Exception exp) { + System.out.println("[SERVER] Caught exception, exiting."); + exp.printStackTrace(System.out); + System.exit(1); + } + } + + 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 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
