change over to use JNDI to get the ConnectionFactory and Queue objects
Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/a6ba5d4f Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/a6ba5d4f Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/a6ba5d4f Branch: refs/heads/master Commit: a6ba5d4f23ba4ba6754eb00cac5ceca0ca73db71 Parents: 630c23b Author: Robert Gemmell <[email protected]> Authored: Tue Feb 3 11:53:41 2015 +0000 Committer: Robert Gemmell <[email protected]> Committed: Tue Feb 3 11:53:41 2015 +0000 ---------------------------------------------------------------------- .../java/org/apache/qpid/jms/example/Drain.java | 21 +++++++++---- .../qpid/jms/example/example-jndi.properties | 31 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/a6ba5d4f/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Drain.java ---------------------------------------------------------------------- diff --git a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Drain.java b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Drain.java index ff1f555..19abd23 100644 --- a/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Drain.java +++ b/qpid-jms-examples/src/main/java/org/apache/qpid/jms/example/Drain.java @@ -22,14 +22,16 @@ package org.apache.qpid.jms.example; import java.util.ArrayList; import java.util.List; +import java.util.Properties; import javax.jms.Connection; +import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; - -import org.apache.qpid.jms.JmsConnectionFactory; +import javax.naming.Context; +import javax.naming.InitialContext; public class Drain { @@ -54,15 +56,24 @@ public class Drain { try { - //TODO: use JNDI lookup rather than direct instantiation - JmsConnectionFactory factory = new JmsConnectionFactory("amqp://" + _hostname + ":" + _port); + // 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.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - Destination queue = session.createQueue("myQueue"); MessageConsumer messageConsumer = session.createConsumer(queue); long start = System.currentTimeMillis(); http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/a6ba5d4f/qpid-jms-examples/src/main/resources/org/apache/qpid/jms/example/example-jndi.properties ---------------------------------------------------------------------- diff --git a/qpid-jms-examples/src/main/resources/org/apache/qpid/jms/example/example-jndi.properties b/qpid-jms-examples/src/main/resources/org/apache/qpid/jms/example/example-jndi.properties new file mode 100644 index 0000000..9faedb8 --- /dev/null +++ b/qpid-jms-examples/src/main/resources/org/apache/qpid/jms/example/example-jndi.properties @@ -0,0 +1,31 @@ +# +# 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. +# + +# Set the InitialContextFactory class to use +java.naming.factory.initial = org.apache.qpid.jms.jndi.JmsInitialContextFactory + +# Define the required ConnectionFactory instances +# connectionfactory.<JNDI-lookup-name> = <URI> +connectionfactory.myFactoryLookup = amqp://localhost:5672 + +# Configure the necessary Queue and Topic objects +# queue.<JNDI-lookup-name> = <queue-name> +# topic.<JNDI-lookup-name> = <topic-name> +queue.myQueueLookup = queue +topic.myTopicLookup = topic \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
