diff options
Diffstat (limited to 'trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java')
-rw-r--r-- | trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java b/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java new file mode 100644 index 0000000000..259756764c --- /dev/null +++ b/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java @@ -0,0 +1,138 @@ +/* + * + * 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.direct; + +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.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; + +/** + * Message producer example, sends message to a queue. + */ +public class Producer +{ + /* Used in log output. */ + private static final String CLASS = "Producer"; + + private int numMessages = 10; + private short deliveryMode = DeliveryMode.NON_PERSISTENT; + + /** + * Create a Producer client. + */ + public Producer () + { + } + + /** + * Run the message producer example. + * @param args Command line arguments. + */ + public static void main(String[] args) + { + Producer producer = new Producer(); + producer.runTest(); + } + + private void runTest() + { + try + { + + // Load JNDI properties + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("direct.properties")); + + //Create the initial context + Context ctx = new InitialContext(properties); + + // look up destination + Destination destination = (Destination)ctx.lookup("directQueue"); + + // Lookup the connection factory + ConnectionFactory conFac = (ConnectionFactory)ctx.lookup("qpidConnectionfactory"); + // create the connection + Connection connection = conFac.createConnection(); + + connection.setExceptionListener(new ExceptionListener() + { + public void onException(JMSException e) + { + e.printStackTrace(); + } + }); + + // Create a session on the connection + // This session is a default choice of non-transacted and uses the auto acknowledge feature of a session. + System.out.println(CLASS + ": Creating a non-transacted, auto-acknowledged session"); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // lookup the queue + //Queue destination = session.createQueue(_queueName); + + // Create a Message producer + System.out.println(CLASS + ": Creating a Message Producer"); + MessageProducer messageProducer = session.createProducer(destination); + + // Create a Message + TextMessage message; + System.out.println(CLASS + ": Creating a TestMessage to send to the destination"); + + // Loop to publish the requested number of messages. + for (int i = 1; i < numMessages + 1; i++) + { + // NOTE: We have NOT HAD TO START THE CONNECTION TO BEGIN SENDING messages, + // this is different to the consumer end as a CONSUMERS CONNECTIONS MUST BE STARTED BEFORE RECEIVING. + message = session.createTextMessage("Message " + i); + System.out.println(CLASS + ": Sending message: " + i); + messageProducer.send(message, deliveryMode, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + } + + // And send a final message to indicate termination. + message = session.createTextMessage("That's all, folks!"); + messageProducer.send(message, deliveryMode, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + + // Close the connection to the broker + System.out.println(CLASS + ": Closing connection"); + connection.close(); + + // Close the JNDI reference + System.out.println(CLASS + ": Closing JNDI context"); + ctx.close(); + } + catch (Exception exp) + { + System.err.println(CLASS + ": Caught an Exception: " + exp); + exp.printStackTrace(); + } + } +} |