add a basic HelloWorld example

Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/819569bd
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/819569bd
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/819569bd

Branch: refs/heads/master
Commit: 819569bdcd3e25013ef3c0e03570ab8332e24549
Parents: 6eeff40
Author: Robert Gemmell <[email protected]>
Authored: Tue Feb 3 12:51:44 2015 +0000
Committer: Robert Gemmell <[email protected]>
Committed: Tue Feb 3 12:51:44 2015 +0000

----------------------------------------------------------------------
 .../org/apache/qpid/jms/example/HelloWorld.java | 88 ++++++++++++++++++++
 1 file changed, 88 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/819569bd/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/HelloWorld.java
----------------------------------------------------------------------
diff --git 
a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/HelloWorld.java 
b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/HelloWorld.java
new file mode 100644
index 0000000..d11a895
--- /dev/null
+++ 
b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/HelloWorld.java
@@ -0,0 +1,88 @@
+/*
+ *
+ * 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 java.util.Properties;
+
+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.naming.Context;
+import javax.naming.InitialContext;
+
+public class HelloWorld {
+    private static final String USER = "guest";
+    private static final String PASSWORD = "guest";
+
+    public static void main(String[] args) throws Exception {
+        try {
+            // JNDI information can be configured by including an file named 
jndi.properties
+            // on the classpath, containing the "java.naming.factory.initial" 
configuration
+            // and properties configuring required ConnectionFactory and 
Destination objects.
+            // The below is an alternative approach being used only for the 
examples.
+            Properties env = new Properties();
+            env.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.apache.qpid.jms.jndi.JmsInitialContextFactory");
+            env.put(Context.PROVIDER_URL, 
ClassLoader.getSystemResource("org/apache/qpid/jms/example/example-jndi.properties").toExternalForm());
+
+            Context context = new InitialContext(env);
+
+            ConnectionFactory factory = (ConnectionFactory) 
context.lookup("myFactoryLookup");
+            Destination queue = (Destination) context.lookup("myQueueLookup");
+
+            Connection connection = factory.createConnection(USER, PASSWORD);
+            connection.setExceptionListener(new MyExceptionListener());
+            connection.start();
+
+            Session session = connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            MessageProducer messageProducer = session.createProducer(queue);
+            MessageConsumer messageConsumer = session.createConsumer(queue);
+
+            TextMessage message = session.createTextMessage("Hello world!");
+            messageProducer.send(message, DeliveryMode.NON_PERSISTENT, 
Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
+            TextMessage receivedMessage = (TextMessage) 
messageConsumer.receive(2000L);
+            System.out.println(receivedMessage.getText());
+
+            connection.close();
+        } catch (Exception exp) {
+            System.out.println("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("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]

Reply via email to