diff options
| author | Jonathan Robie <jonathan@apache.org> | 2010-04-16 18:32:16 +0000 |
|---|---|---|
| committer | Jonathan Robie <jonathan@apache.org> | 2010-04-16 18:32:16 +0000 |
| commit | d7bf11f4fdb6f5223ed39c3e59c76da9afa7e505 (patch) | |
| tree | 2b5a761f5af25ac3295bd836939c0bb73d30e9ca /java/client/example/src/main | |
| parent | 3485fe658a38634178747791cf1575c0ec80e9f6 (diff) | |
| download | qpid-python-d7bf11f4fdb6f5223ed39c3e59c76da9afa7e505.tar.gz | |
"Hello world!" example in Java
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@935034 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/client/example/src/main')
2 files changed, 110 insertions, 0 deletions
diff --git a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/hello/Hello.java b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/hello/Hello.java new file mode 100644 index 0000000000..4eac17f380 --- /dev/null +++ b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/hello/Hello.java @@ -0,0 +1,83 @@ +/* + * + * 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.example.jmsexample.hello; + +import javax.jms.*; +import javax.naming.Context; +import javax.naming.InitialContext; +import java.util.Properties; + +/** + * Message producer example, sends message to a queue. + */ +public class Hello { + private static final String CLASS = "Hello"; + + + public Hello() { + } + + public static void main(String[] args) { + Hello producer = new Hello(); + producer.runTest(); + } + + private void runTest() { + try { + // Create JNDI context + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("hello.properties")); + Context context = new InitialContext(properties); + + ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionfactory"); + Connection connection = connectionFactory.createConnection(); + connection.setExceptionListener(new ExceptionListener() { + public void onException(JMSException e) { + e.printStackTrace(); + } + }); + + Session session = connection.createSession(); + Destination destination = (Destination) context.lookup("topicExchange"); + + MessageProducer messageProducer = session.createProducer(destination); + MessageConsumer messageConsumer = session.createConsumer(destination); + + Message message = session.createTextMessage("Hello world!"); + messageProducer.send(message); + + message = messageConsumer.receive(); + if (message instanceof TextMessage) { + String text = ((TextMessage) message).getText(); + System.out.println(text); + } else { + System.out.println("Ooops, not a TextMessage!"); + } + + connection.close(); + context.close(); + } + catch (Exception exp) { + System.err.println(CLASS + ": Caught an Exception: " + exp); + exp.printStackTrace(); + } + } +} diff --git a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/hello/hello.properties b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/hello/hello.properties new file mode 100644 index 0000000000..839439ae4a --- /dev/null +++ b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/hello/hello.properties @@ -0,0 +1,27 @@ +# +# 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. +# +java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory + +# register some connection factories +# connectionfactory.[jndiname] = [ConnectionURL] +connectionfactory.qpidConnectionfactory = amqp://guest:guest@clientid/test?brokerlist='tcp://localhost:5672' + +# Register an AMQP destination in JNDI +# destination.[jniName] = [BindingURL] +destination.topicExchange = amq.topic |
