﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Sun River-随笔分类-JMS</title><link>http://www.blogjava.net/SunRiver/category/15316.html</link><description>Topics about J2EE XML AJAX SOA DB etc.</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 03:11:12 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 03:11:12 GMT</pubDate><ttl>60</ttl><item><title>The basic steps to write a JMS program</title><link>http://www.blogjava.net/SunRiver/archive/2006/10/30/77948.html</link><dc:creator>Sun River</dc:creator><author>Sun River</author><pubDate>Sun, 29 Oct 2006 17:11:00 GMT</pubDate><guid>http://www.blogjava.net/SunRiver/archive/2006/10/30/77948.html</guid><wfw:comment>http://www.blogjava.net/SunRiver/comments/77948.html</wfw:comment><comments>http://www.blogjava.net/SunRiver/archive/2006/10/30/77948.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/SunRiver/comments/commentRss/77948.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/SunRiver/services/trackbacks/77948.html</trackback:ping><description><![CDATA[
		<font face="Times New Roman" size="3">
				<font face="Times New Roman" color="#4c1900" size="5">
						<p align="left">1). Steps for Building a JMS Sender Application</p>
						<p align="left">
						</p>
				</font>1.Get ConnectionFactory and Destination object (Topic or Queue) through JNDI<strong><font face="Arial" color="#0000ff" size="1">          <p class="MsoNormal" style="MARGIN: 0in 0in 0pt 81pt; mso-layout-grid-align: none"><b><span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: Arial"><font size="3">// Get JNDI InitialContext object<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p></o:p></font></span></b></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 81pt; mso-layout-grid-align: none"><b><span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Arial"><font size="3">Context jndiContext = new InitialContext();<o:p></o:p></font></span></b></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 81pt; mso-layout-grid-align: none"><b><span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: Arial"><font size="3">// Locate ConnectionFactory object via JNDI<o:p></o:p></font></span></b></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 81pt; mso-layout-grid-align: none"><font size="3"><b><span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Arial">TopicConnectionFactory factory = <br /></span></b><b><span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Arial">      (TopicConnectionFactory) jndiContext.lookup(</span></b><b><span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Arial">"MyTopicConnectionFactory");<o:p></o:p></span></b></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 81pt; mso-layout-grid-align: none"><font size="3"><b><span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: Arial">// Locate Destination object (Topic or Queue)</span></b><b><span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: Arial"> through JNDI<o:p></o:p></span></b></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 81pt; mso-layout-grid-align: none"><font size="3"><b><span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Arial">Topic weatherTopic = </span></b><b><span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Arial">(Topic) jndiContext.lookup("WeatherData");</span></b><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial"><o:p></o:p></span></font></p><p align="left"></p></font></strong>2.Create a Connection<b><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial">                              <br />                                 <br /><font size="4">                    </font></span></b><font size="3"><b><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><b><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial">// Create a Connection object from ConnectionFactory object<br /></span></b>                                 TopicConnection topicConnection = factory.createTopicConnection();</span></b><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial"><o:p></o:p></span></font><p align="left">3.Create a Session to send/receive messages<br /><br /></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 45pt; mso-layout-grid-align: none"><font size="2"><b><span style="FONT-SIZE: 10.5pt; COLOR: blue; FONT-FAMILY: Arial">         /</span></b><b><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial">/<font size="4"> Create a Session from Connection object.<o:p></o:p></font></span></b></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 45pt; mso-layout-grid-align: none"><font size="4"><b><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial">            // 1</span></b><b><span style="FONT-SIZE: 5.5pt; COLOR: blue; FONT-FAMILY: Arial">st </span></b><b><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial">parameter controls transaction<o:p></o:p></span></b></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 45pt; mso-layout-grid-align: none"><font size="4"><b><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial">            // 2</span></b><b><span style="FONT-SIZE: 5.5pt; COLOR: blue; FONT-FAMILY: Arial">nd </span></b><b><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial">parameter specifies acknowledgment type<o:p></o:p></span></b></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 45pt; mso-layout-grid-align: none"><b><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><font size="4">         TopicSession session =<o:p></o:p></font></span></b></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt 45pt; mso-layout-grid-align: none"><font size="4"><b><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">            topicConnection.createTopicSession (false, Session.CLIENT_ACKNOWLEDGE);</span></b><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial"><o:p></o:p></span></font></p><p align="left">4.Create a MessageProducer (TopicPublisher or QueueSender)<br /><b><font face="Arial" color="#0000ff" size="1"><br />                                          <font size="2">// Create MessageProducer from Session object<br /></font><font size="2">                              // TopicPublisher for Pub/Sub<br /></font><font size="2">                              // QueueSender for Point-to-Point<br /></font></font><font face="Arial"><font size="2">                              TopicPublisher publisher =session.createPublisher(weatherTopic);</font></font></b></p></font>
		<p align="left">5.Start Connection<br /><b><font face="Arial" color="#0000ff" size="1">                                                <font size="3">// Until Connection gets started, message flow<br />                                    // is inhibited: Connection must be started before<br />                                    // messages will be transmitted.<br /></font></font><font face="Arial" size="1"><font size="3">                                    topicConnection.start();</font></font></b></p>
		<p align="left">6.Send (publish) messages<br /><b><font face="Arial" color="#0000ff" size="1"><font size="3">                                    // Create a Message<br /></font></font><font face="Arial">                                     TextMessage message =session.createMessage();<br />                                    message.setText("text:35 degrees");<br /></font><font face="Arial" color="#0000ff">                                    // Publish the message<br /></font><font face="Arial" size="1"><font size="3">                                    publisher.publish(message);</font></font></b></p>
		<p align="left">7.Close Session and Connection<font face="Times New Roman" color="#4c1900" size="5"></font></p>
		<p align="left">2). Steps for Building a JMS Receiver Application (non-blocking mode)<br /><font face="Times New Roman" size="3"></font></p>
		<p align="left">1.Get ConnectionFactory and Destination object</p>
		<p align="left">(Topic or Queue) through JNDI</p>
		<p align="left">2.Create a Connection</p>
		<p align="left">3.Create a Session to send/receive messages</p>
		<p align="left">4.<font face="Times New Roman" color="#ff0000" size="3">Create a MessageConsumer (TopicSubscriber or QueueReceiver)<br /><b><font face="Arial" color="#0000ff" size="1"></font></b></font></p>
		<p align="left">     <font size="3">       <font color="#0000ff">// Create Subscriber from Session object<br /></font></font><font face="Arial"><font color="#0000ff">            TopicSubscriber subscriber =session.createSubscriber(weatherTopic);<br /></font></font><font face="Times New Roman"><br /><font size="3">5.</font></font><font face="Times New Roman" color="#ff0000"><font size="3">Register MessageListener for non-blocking mode<br /><b><font face="Arial" size="1">         <font color="#0000ff" size="3">WeatherListener myListener= new WeatherListener();<br /></font></font><font color="#0000ff"><font face="Arial">         // Register MessageListener with TopicSubscriber object<br /></font><font face="Arial">         subscriber.setMessageListener(myListener);</font></font></b></font></font></p>
		<font face="Times New Roman">
				<p align="left">
						<font size="3">6.Start Connection</font>
				</p>
				<p align="left">
						<font size="3">7.Close Session and Connection<br /><font face="Times New Roman" color="#4c1900" size="5"></font></font>
				</p>
				<p align="left">3). Steps for Building a JMS Receiver Application for <font face="Times New Roman" color="#4c1900" size="5">blocking</font> mode)<br /><font face="Times New Roman"><font size="2"><br />1</font><font size="3">.Get ConnectionFactory and Destination </font><font size="3">object (Topic or Queue) through JNDI</font></font></p>
				<p align="left">
						<font size="3">2.Create a Connection</font>
				</p>
				<p align="left">
						<font size="3">3.Create a Session to send/receive messages</font>
				</p>
				<p align="left">
						<font size="3">4.</font>
				</p>
		</font>
		<font face="Times New Roman" color="#ff0000">
				<font size="3">Create a MessageConsumer</font>
		</font>
		<font face="Times New Roman">
				<p align="left">
						<font size="3">5.Start Connection</font>
				</p>
				<p align="left">
						<font size="3">6.</font>
				</p>
		</font>
		<font face="Times New Roman" color="#ff0000">
				<font size="3">Receive message</font>
		</font>
		<font face="Times New Roman" size="3">
				<p align="left">7.Close Session and Connection</p>
		</font>
<img src ="http://www.blogjava.net/SunRiver/aggbug/77948.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/SunRiver/" target="_blank">Sun River</a> 2006-10-30 01:11 <a href="http://www.blogjava.net/SunRiver/archive/2006/10/30/77948.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>