/
readme.html
101 lines (79 loc) · 3.61 KB
/
readme.html
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
<html>
<head>
<title>JBoss Messaging Java EE Servlet Example</title>
<link rel="stylesheet" type="text/css" href="../../common/common.css">
</head>
<body>
<h1>Java EE Servlet Example</h1>
<br>
<p>This example shows you how to configure and use servlet transport with JBoss Messaging.</p>
<h2>JBoss AS configuration</h2>
<p>Please refer to JBoss Messaging Quickstart guide to <a href="../../../docs/quickstartguide/en/html_single/index.html#installation.jboss">install it in JBoss AS 5<a></p>
<h2>Example configuration</h2>
<h2>Example step-by-step</h2>
<p><em>To deploy and start the server, type <code>ant deploy</code>from the example directory</br >
Once the server has started type <code>ant run</code> to run the example.<br />
To remove the new profile type <code>ant undeploy</code>.</em></p>
<br>
<ol>
<li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>jndi.properties</code> file in the directory <code>config</code></li>
<pre>
<code>initialContext = new InitialContext();</code>
</pre>
<li>We look up the JMS queue object from JNDI</li>
<pre>
<code>Queue queue = (Queue) initialContext.lookup("/queue/testQueue");</code>
</pre>
<li>We look up the JMS connection factory object from JNDI</li>
<pre>
<code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/TestServletConnectionFactory");</code>
</pre>
<li>We create a JMS connection</li>
<pre>
<code>connection = cf.createConnection();</code>
</pre>
<li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
<pre>
<code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>We create a JMS message producer on the session. This will be used to send the messages.</li>
<pre>
<code>MessageProducer messageProducer = session.createProducer(queue);</code>
</pre>
<li>We create a JMS text messages that we are going to send.</li>
<pre>
<code> TextMessage message = session.createTextMessage("This is a text message");</code>
</pre>
<li>We send messages to the queue</li>
<pre>
<code>messageProducer.send(message);</code>
</pre>
<li>We create a JMS Message Consumer</li>
<pre>
<code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
</pre>
<li>We start the connection</li>
<pre>
<code>connection.start();</code>
</pre>
<li>We receive the message</li>
<pre>
<code>TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);</code>
</pre>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre>
<code>finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
</body>
</html>