summaryrefslogtreecommitdiff
path: root/documentation/content/xdocs/Using Qpid with other JNDI Providers.html
blob: 0b46fc3a3aafed7700183e3f707ff58ed83a13f1 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
<html>
    <head>
        <title>Apache Qpid : Using Qpid with other JNDI Providers</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 : Using Qpid with other JNDI Providers
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Jun 22, 2007 by <font color="#0050B2">ritchiem</font>.
				    </div>

				    
<h2><a name="UsingQpidwithotherJNDIProviders-HowtouseaJNDIProvider"></a>How to use a JNDI Provider</h2>

<p>Qpid will work with any JNDI provider capable of storing Java objects. We have a task to add our own initial context factory, but until that's available ....</p>

<p>First you must select a JNDI provider to use. If you aren't already using an application server (i.e. Tomcat ?) which provides JNDI support you could consider using either:</p>
<ul>
	<li>Apache's <a href="http://directory.apache.org/subprojects/apacheds/index.html" title="Visit page outside Confluence">Directory</a> which provides an LDAP JNDI implementation</li>
</ul>


<ul>
	<li>OR the SUN JNDI SPI for the FileSystem which can be downloaded from <a href="http://java.sun.com/products/jndi/downloads/index.html" title="Visit page outside Confluence">http://java.sun.com/products/jndi/downloads/index.html</a></li>
</ul>


<ul class="alternate" type="square">
	<li>Click : Download JNDI 1.2.1 &amp; More button</li>
	<li>Download: File System Service Provider, 1.2 Beta 3</li>
	<li>and then add the two jars in the lib dir to your class path.</li>
</ul>


<p>There are two steps to using JNDI objects.</p>
<ul class="alternate" type="square">
	<li>Bind : Which stores a reference to a JMS Object in the provider.</li>
	<li>Lookup : Which tries to retrieve the reference and create the JMS Object.</li>
</ul>


<p>There are two objects that would normally be stored in JNDI.</p>
<ul class="alternate" type="square">
	<li>A ConnectionFactory</li>
	<li>A Destination (Queue or Topic)</li>
</ul>


<h3><a name="UsingQpidwithotherJNDIProviders-Binding"></a>Binding</h3>

<p>Then you need to setup the values that the JNDI provider will used to bind your references, something like this:</p>
<div class="code" style="border-style: solid; "><div class="codeHeader" style="border-bottom-style: solid; "><b>Setup JNDI</b></div><div class="codeContent">
<pre class="code-java">Hashtable env = <span class="code-keyword">new</span> Hashtable(11);
  env.put(Context.INITIAL_CONTEXT_FACTORY,<span class="code-quote">"com.sun.jndi.fscontext.RefFSContextFactory"</span>);
  env.put(Context.PROVIDER_URL,LOCAL_FILE_PATH_FOR_STORING_BINDS_PATH_MUST_EXIST);</pre>
</div></div>
<p>These values are then used to create a context to bind your references.</p>
<div class="code" style="border-style: solid; "><div class="codeHeader" style="border-bottom-style: solid; "><b>Perform Binding of ConnectionFactory</b></div><div class="codeContent">
<pre class="code-java"><span class="code-keyword">try</span>
{
    Context ctx = <span class="code-keyword">new</span> InitialContext(env);

    <span class="code-comment">// Create the object to be bound in <span class="code-keyword">this</span> <span class="code-keyword">case</span> a ConnectionFactory
</span>    ConnectionFactory factory = <span class="code-keyword">null</span>;

    <span class="code-keyword">try</span>
    {
        factory = <span class="code-keyword">new</span> AMQConnectionFactory(CONNECTION_URL);
        <span class="code-keyword">try</span>
        {
            ctx.bind(binding, factory);
        }
        <span class="code-keyword">catch</span> (NamingException e)
        {
            <span class="code-comment">//Handle problems with binding. Such as the binding already exists.
</span>        }
    }
    <span class="code-keyword">catch</span> (URLSyntaxException amqe)
    {
        <span class="code-comment">//Handle any exception with creating ConnnectionFactory
</span>    }
}
<span class="code-keyword">catch</span> (NamingException e)
{
    <span class="code-comment">//Handle problem creating the Context.
</span>}</pre>
</div></div>
<p>To bind a queue instead simply create a AMQQueue object and use that in the binding call.</p>
<div class="code" style="border-style: solid; "><div class="codeHeader" style="border-bottom-style: solid; "><b>Bind a AMQQueue</b></div><div class="codeContent">
<pre class="code-java">AMQQueue  queue = <span class="code-keyword">new</span> AMQQueue(QUEUE_URL);
ctx.bind(binding, queue);</pre>
</div></div>

<h3><a name="UsingQpidwithotherJNDIProviders-Lookup"></a>Lookup</h3>

<p>You can then get a queue connection factory from the JNDI context.</p>
<div class="code" style="border-style: solid; "><div class="codeHeader" style="border-bottom-style: solid; "><b>Perform Binding of ConnectionFactory</b></div><div class="codeContent">
<pre class="code-java">ConnectionFactory factory;
<span class="code-keyword">try</span>
{
    factory= (ConnectionFactory)ctx.lookup(binding);
}
<span class="code-keyword">catch</span> (NamingException e)
{
    <span class="code-comment">//Handle problems with lookup. Such as binding does not exist.
</span>}</pre>
</div></div>
<p>Note that you need not cast the bound object back to an <tt>AMQConnectionFactory</tt> so all your current JMS apps that use JNDI can start using Qpid straight away.</p>


<h2><a name="UsingQpidwithotherJNDIProviders-HowtocreateaTopicConnectionFactoryandQueueConnectionFactory"></a>How to create a TopicConnectionFactory and QueueConnectionFactory</h2>

<p>AMQConnectionFactory implements TopicConnectionFactory and QueueConnectionFactory as well as the ConnectionFactory.</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>