From 2d92397a1209bf4c4645d76e3104abd1e58a2d66 Mon Sep 17 00:00:00 2001 From: Rajith Muditha Attapattu Date: Wed, 26 May 2010 23:41:46 +0000 Subject: Added examples to match the c++ and python clients. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@948636 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/qpid/example/Drain.java | 86 ++++++ .../main/java/org/apache/qpid/example/Hello.java | 74 +++++ .../java/org/apache/qpid/example/MapReceiver.java | 52 ++++ .../java/org/apache/qpid/example/MapSender.java | 83 +++++ .../java/org/apache/qpid/example/OptionParser.java | 335 +++++++++++++++++++++ .../main/java/org/apache/qpid/example/Spout.java | 148 +++++++++ .../java/org/apache/qpid/example/hello.properties | 27 ++ 7 files changed, 805 insertions(+) create mode 100644 java/client/example/src/main/java/org/apache/qpid/example/Drain.java create mode 100644 java/client/example/src/main/java/org/apache/qpid/example/Hello.java create mode 100644 java/client/example/src/main/java/org/apache/qpid/example/MapReceiver.java create mode 100644 java/client/example/src/main/java/org/apache/qpid/example/MapSender.java create mode 100644 java/client/example/src/main/java/org/apache/qpid/example/OptionParser.java create mode 100644 java/client/example/src/main/java/org/apache/qpid/example/Spout.java create mode 100644 java/client/example/src/main/java/org/apache/qpid/example/hello.properties (limited to 'java/client/example/src') diff --git a/java/client/example/src/main/java/org/apache/qpid/example/Drain.java b/java/client/example/src/main/java/org/apache/qpid/example/Drain.java new file mode 100644 index 0000000000..51491326c4 --- /dev/null +++ b/java/client/example/src/main/java/org/apache/qpid/example/Drain.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.example; + +import javax.jms.Connection; +import javax.jms.Destination; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.Session; + +import org.apache.qpid.client.AMQAnyDestination; +import org.apache.qpid.client.AMQConnection; +import org.slf4j.Logger; + +public class Drain extends OptionParser +{ + + static final Option FOREVER = new Option("f", + "forever", + "ignore timeout and wait forever", + null, + null, + Boolean.class); + + static + { + optDefs.add(BROKER); + optDefs.add(HELP); + optDefs.add(TIMEOUT); + optDefs.add(FOREVER); + optDefs.add(CON_OPTIONS); + optDefs.add(BROKER_OPTIONS); + } + + public Drain(String[] args, String usage, String desc) throws Exception + { + super(args, usage, desc); + + Connection con = createConnection(); + con.start(); + Session ssn = con.createSession(false,Session.AUTO_ACKNOWLEDGE); + Destination dest = new AMQAnyDestination(address); + MessageConsumer consumer = ssn.createConsumer(dest); + Message msg; + + long timeout = -1; + if (containsOp(TIMEOUT)) { timeout = Integer.parseInt(getOp(TIMEOUT))*1000; } + if (containsOp(FOREVER)) { timeout = 0; } + + while ((msg = consumer.receive(timeout)) != null) + { + System.out.println("\n------------- Msg -------------"); + System.out.println(msg); + System.out.println("-------------------------------\n"); + } + + ssn.close(); + con.close(); + } + + public static void main(String[] args) throws Exception + { + String u = "Usage: drain [OPTIONS] 'ADDRESS'"; + String d = "Drains messages from the specified address."; + + Drain drain = new Drain(args,u,d); + } +} diff --git a/java/client/example/src/main/java/org/apache/qpid/example/Hello.java b/java/client/example/src/main/java/org/apache/qpid/example/Hello.java new file mode 100644 index 0000000000..949ee4dac6 --- /dev/null +++ b/java/client/example/src/main/java/org/apache/qpid/example/Hello.java @@ -0,0 +1,74 @@ +/* + * + * 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; + +import javax.jms.*; +import javax.naming.Context; +import javax.naming.InitialContext; +import java.util.Properties; + + +public class Hello +{ + + public Hello() + { + } + + public static void main(String[] args) + { + Hello hello = new Hello(); + hello.runTest(); + } + + private void runTest() + { + try { + 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.start(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination destination = (Destination) context.lookup("topicExchange"); + + MessageProducer messageProducer = session.createProducer(destination); + MessageConsumer messageConsumer = session.createConsumer(destination); + + TextMessage message = session.createTextMessage("Hello world!"); + messageProducer.send(message); + + message = (TextMessage)messageConsumer.receive(); + System.out.println(message.getText()); + + connection.close(); + context.close(); + } + catch (Exception exp) + { + exp.printStackTrace(); + } + } +} diff --git a/java/client/example/src/main/java/org/apache/qpid/example/MapReceiver.java b/java/client/example/src/main/java/org/apache/qpid/example/MapReceiver.java new file mode 100644 index 0000000000..89db04f8d3 --- /dev/null +++ b/java/client/example/src/main/java/org/apache/qpid/example/MapReceiver.java @@ -0,0 +1,52 @@ +/* + * + * 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; + +import javax.jms.Connection; +import javax.jms.Destination; +import javax.jms.MapMessage; +import javax.jms.MessageConsumer; +import javax.jms.Session; + +import org.apache.qpid.client.AMQAnyDestination; +import org.apache.qpid.client.AMQConnection; + + +public class MapReceiver { + + public static void main(String[] args) throws Exception + { + Connection connection = + new AMQConnection("amqp://guest:guest@test/?brokerlist='tcp://localhost:5672'"); + + connection.start(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination queue = new AMQAnyDestination("ADDR:message_queue; {create: always}"); + MessageConsumer consumer = session.createConsumer(queue); + + MapMessage m = (MapMessage)consumer.receive(); + System.out.println(m); + connection.close(); + } + +} \ No newline at end of file diff --git a/java/client/example/src/main/java/org/apache/qpid/example/MapSender.java b/java/client/example/src/main/java/org/apache/qpid/example/MapSender.java new file mode 100644 index 0000000000..0ce9383add --- /dev/null +++ b/java/client/example/src/main/java/org/apache/qpid/example/MapSender.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; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.jms.Connection; +import javax.jms.Destination; +import javax.jms.MapMessage; +import javax.jms.MessageProducer; +import javax.jms.Session; + +import org.apache.qpid.client.AMQAnyDestination; +import org.apache.qpid.client.AMQConnection; + + +public class MapSender { + + public static void main(String[] args) throws Exception + { + Connection connection = + new AMQConnection("amqp://guest:guest@test/?brokerlist='tcp://localhost:5672'"); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination queue = new AMQAnyDestination("ADDR:message_queue; {create: always}"); + MessageProducer producer = session.createProducer(queue); + + MapMessage m = session.createMapMessage(); + m.setIntProperty("Id", 987654321); + m.setStringProperty("name", "Widget"); + m.setDoubleProperty("price", 0.99); + + List colors = new ArrayList(); + colors.add("red"); + colors.add("green"); + colors.add("white"); + m.setObject("colours", colors); + + Map dimensions = new HashMap(); + dimensions.put("length",10.2); + dimensions.put("width",5.1); + dimensions.put("depth",2.0); + m.setObject("dimensions",dimensions); + + List> parts = new ArrayList>(); + parts.add(Arrays.asList(new Integer[] {1,2,5})); + parts.add(Arrays.asList(new Integer[] {8,2,5})); + m.setObject("parts", parts); + + Map specs = new HashMap(); + specs.put("colours", colors); + specs.put("dimensions", dimensions); + specs.put("parts", parts); + m.setObject("specs",specs); + + producer.send(m); + connection.close(); + } + +} \ No newline at end of file diff --git a/java/client/example/src/main/java/org/apache/qpid/example/OptionParser.java b/java/client/example/src/main/java/org/apache/qpid/example/OptionParser.java new file mode 100644 index 0000000000..6b1f514258 --- /dev/null +++ b/java/client/example/src/main/java/org/apache/qpid/example/OptionParser.java @@ -0,0 +1,335 @@ +/* + * + * 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; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.jms.Connection; + +import org.apache.qpid.client.AMQConnection; + +public class OptionParser +{ + static final Option BROKER = new Option("b", + "broker", + "connect to specified broker", + "USER:PASS@HOST:PORT", + "guest:guest@localhost:5672", + String.class); + + static final Option HELP = new Option("h", + "help", + "show this help message and exit", + null, + null, + Boolean.class); + + static final Option TIMEOUT = new Option("t", + "timeout", + "timeout in seconds to wait before exiting", + "TIMEOUT", + "0", + Integer.class); + + static final Option CON_OPTIONS = new Option(null, + "con-option", + "JMS Connection URL options. Ex sync_ack=true sync_publish=all ", + "NAME=VALUE", + null, + String.class); + + + static final Option BROKER_OPTIONS = new Option(null, + "broker-option", + "JMS Broker URL options. Ex ssl=true sasl_mechs=GSSAPI ", + "NAME=VALUE", + null, + String.class); + + + protected Map optMap = new HashMap(); + protected static List