summaryrefslogtreecommitdiff
path: root/content/xdocs/How to Use JNDI.html
blob: 45f284d965032adfc6490f4af49b69a1dce7f1f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<html>
    <head>
        <title>Apache Qpid : How to Use JNDI</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            Apache Qpid : How to Use JNDI
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Apr 02, 2008 by <font color="#0050B2">asimon</font>.
				    </div>

				    <h2><a name="HowtoUseJNDI-HowtousethePropertiesFileInitialContextFactory"></a>How to use the PropertiesFileInitialContextFactory</h2>

<p>This ContextFactory uses a java properties file to setup initial values. </p>

<p>This is the example properties file.</p>

<div class="preformatted"><div class="preformattedContent">
<pre>java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory

# use the following property to configure the default connector
#java.naming.provider.url - ignored.

# register some connection factories
# connectionfactory.[jndiname] = [ConnectionURL]
connectionfactory.local = amqp://guest:guest@clientid/testpath?brokerlist='vm://:1'

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue

# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.ibmStocks = stocks.nyse.ibm

# Register an AMQP destination in JNDI
#   NOTE: Qpid currently only supports direct,topics and headers
# destination.[jniName] = [BindingURL]
destination.direct = direct://amq.direct//directQueue
</pre>
</div></div>


<p>The property file allows a number of queues to be defined that can then be discovered via JNDI. There are four properties used by the PFICFactory. <br/>
<em>connectionfactory.&lt;jndiname&gt;</em> this is the <a href="Connection URL Format.html" title="Connection URL Format">Connection URL</a> that the connection factory will use to perform connections.<br/>
<em>queue.&lt;jndiname&gt;</em> this defines a jms queue or in amqp a amq.direct exchange<br/>
<em>topic.&lt;jndiname&gt;</em> this defines a jms topic or in amqp a amq.topic exchange<br/>
<em>destination.&lt;jndiname&gt;</em> this takes a <a href="BindingURLFormat.html" title="BindingURLFormat">Binding URL</a> and so can be used for defining all amq destinations, queues, topics and header matching.</p>

<p>In all of these properties the <em>&lt;jndiname&gt;</em> is the string value that would be given when performing a lookup.</p>


<p><b>NOTE</b>: This does not create the queue on the broker. You should ensure that you have created the queue before publishing to it. Queues can be declared in the virtualhosts.xml file so that they are created on broker startup, or created dynamically by consuming clients. Topics and other destinations that use temporary queues cannot be created in this way, so a consumer must be created first before publishing messages with mandatory routing.</p>

<h3><a name="HowtoUseJNDI-Examplelookupcode"></a>Example lookup code</h3>

<p>The <em>bindingValue</em> is the String that would be placed in <em>&lt;jndiname&gt;</em> above.</p>

<div class="code" style="border-style: solid; "><div class="codeHeader" style="border-bottom-style: solid; "><b>Simple JNDI lookup using files</b></div><div class="codeContent">
<pre class="code-java"><span class="code-comment">// Load the properties file ... 
</span>Properties properties = <span class="code-keyword">new</span> Properties();
properties.load(propertiesfile_inputStream);

<span class="code-comment">// Create the initial context
</span>Context ctx = <span class="code-keyword">new</span> InitialContext(properties);

<span class="code-comment">// Perform the binds
</span>object = ctx.lookup(bindingValue);

<span class="code-comment">// Close the context when we're done
</span>ctx.close();</pre>
</div></div>

<div class="code" style="border-style: solid; "><div class="codeHeader" style="border-bottom-style: solid; "><b>Simple JNDI lookup using properties</b></div><div class="codeContent">
<pre class="code-java"><span class="code-keyword">final</span> <span class="code-object">String</span> INITIAL_CONTEXT_FACTORY = <span class="code-quote">"org.apache.qpid.jndi.PropertiesFileInitialContextFactory"</span>;

<span class="code-keyword">final</span> <span class="code-object">String</span> CONNECTION_JNDI_NAME = <span class="code-quote">"local"</span>;
<span class="code-keyword">final</span> <span class="code-object">String</span> CONNECTION_NAME = <span class="code-quote">"amqp:<span class="code-comment">//guest:guest@clientid/testpath?brokerlist='vm://:1'"</span>;
</span>
<span class="code-keyword">final</span> <span class="code-object">String</span> QUEUE_JNDI_NAME = <span class="code-quote">"queue"</span>;
<span class="code-keyword">final</span> <span class="code-object">String</span> QUEUE_NAME = <span class="code-quote">"example.MyQueue"</span>;

<span class="code-comment">// Set the properties ... 
</span>Properties properties = <span class="code-keyword">new</span> Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
properties.put(<span class="code-quote">"connectionfactory."</span>+CONNECTION_JNDI_NAME , CONNECTION_NAME);
properties.put(<span class="code-quote">"queue."</span>+QUEUE_JNDI_NAME , QUEUE_NAME);

<span class="code-comment">// Create the initial context
</span>Context ctx = <span class="code-keyword">new</span> InitialContext(properties);

<span class="code-comment">// Perform the lookups
</span>ConnectionFactory factory = (ConnectionFactory)ctx.lookup(CONNECTION_JNDI_NAME);
Queue queue = (Queue)ctx.lookup(QUEUE_JNDI_NAME);

<span class="code-comment">// Close the context when we're done
</span>ctx.close();</pre>
</div></div>

<p><a href="Using Qpid with other JNDI Providers.html" title="Using Qpid with other JNDI Providers">Using Qpid with other JNDI Providers</a></p>

				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Apr 22, 2008 02:47</font></td>
		    </tr>
	    </table>
    </body>
</html>