<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brucalipto.org &#187; JMS</title>
	<atom:link href="http://www.brucalipto.org/tag/jms/feed" rel="self" type="application/rss+xml" />
	<link>http://www.brucalipto.org</link>
	<description>Tenete la porta aperta...</description>
	<lastBuildDate>Thu, 24 Nov 2011 08:59:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Implementazione di un QueueRequestor con timeout.</title>
		<link>http://www.brucalipto.org/java/implementazione-di-un-queuerequestor-con-timeout</link>
		<comments>http://www.brucalipto.org/java/implementazione-di-un-queuerequestor-con-timeout#comments</comments>
		<pubDate>Mon, 26 Apr 2004 15:45:10 +0000</pubDate>
		<dc:creator>apiero</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JMS]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/unclassified/implementazione-di-un-queuerequestor-con-timeout</guid>
		<description><![CDATA[Mi sono trovato nella necessità di dover usare delle code JMS per lo scambio di messaggi tra due applicazioni. L&#8217;idea era che a domanda doveva seguire una risposta in maniera sincrona e le API javax.jms mettono a disposizione per lo scopo l&#8217;oggetto javax.jms.QueueRequestor. Il problema Il problema del QueueRequestor è che non ha un timeout [...]]]></description>
			<content:encoded><![CDATA[<p>Mi sono trovato nella necessità di dover usare delle code JMS per lo scambio di messaggi tra due applicazioni. L&#8217;idea era che a domanda doveva seguire una risposta in maniera sincrona e le API javax.jms mettono a disposizione per lo scopo l&#8217;oggetto <em>javax.jms.QueueRequestor</em>.<span id="more-7"></span></p>
<h2>Il problema</h2>
<p>Il problema del QueueRequestor è che non ha un timeout dopo il quale ritorna in qualsiasi caso: se non si ottiene una risposta                         il QueueRequestor non fa procedere l&#8217;esecuzione e ciò non è bello.</p>
<h2>La soluzione</h2>
<p>Per la soluzione sono stato illuminato da <a href="http://groups.google.com/groups?hl=it&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;threadm=3D9D9C2B.A49F993F%40replyinnewsgroup.com&amp;rnum=1&amp;prev=/groups%3Fq%3Dqueuerequestor%2520timeout%2520%26hl%3Dit%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26sa%3DN%26tab%3Dwg">questo</a>                         post sui newsgroup: qui si afferma che il QueueRequestor è nient&#8217;altro che un wrapper di una serie di API pubbliche del package <em>javax.jms</em>.<br />
Ecco una classe d&#8217;esempio che si interfaccia ad una coda JMS Tibco:<br />
import javax.jms.*;<br />
import java.io.Serializable;<br />
import com.tibco.tibjms.TibjmsQueueConnectionFactory;</p>
<p>public class QueueRequestorImpl<br />
{<br />
private final static String QUEUE_PATH = &#8220;tcp://localhost:7222&#8243;;<br />
private final static String QUEUE_NAME = &#8220;testQueue&#8221;;<br />
private final static long   QUEUE_TIMEOUT = 10000;</p>
<p>public static Object connect(Serializable request)<br />
{<br />
QueueConnection queueConnection = null;<br />
QueueSession queueSession = null;<br />
Queue queue = null;<br />
ObjectMessage objmessage = null;</p>
<p>TemporaryQueue tempQueue = null;<br />
QueueSender qSender = null;<br />
QueueReceiver qReceiver = null;</p>
<p>try<br />
{<br />
QueueConnectionFactory queueConnectionFactory = null;<br />
queueConnectionFactory = new TibjmsQueueConnectionFactory(QUEUE_PATH);<br />
queueConnection = queueConnectionFactory.createQueueConnection();<br />
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);<br />
queue = getQueue(QUEUE_NAME, queueSession);</p>
<p>tempQueue = queueSession.createTemporaryQueue();<br />
qReceiver = queueSession.createReceiver(tempQueue);<br />
qSender = queueSession.createSender(queue);<br />
}<br />
catch (Exception e)<br />
{<br />
return null;<br />
}<br />
finally<br />
{<br />
try<br />
{<br />
if (qReceiver!=null) qReceiver.close();<br />
if (qSender!=null) qSender.close();<br />
if (tempQueue!=null) tempQueue.delete();<br />
if (queueConnection!=null) queueConnection.close();<br />
}catch (Exception e){;}<br />
}<br />
try<br />
{<br />
objmessage = queueSession.createObjectMessage();<br />
objmessage.setJMSReplyTo(tempQueue);<br />
queueConnection.start();<br />
objmessage.setObject(request);</p>
<p>long elapsedTime = System.currentTimeMillis();<br />
qSender.send(objmessage);<br />
Message response = qReceiver.receive(QUEUE_TIMEOUT);<br />
elapsedTime = System.currentTimeMillis() &#8211; elapsedTime;<br />
System.out.println(&#8220;QueueRequestorImpl Response Time: &#8221; + elapsedTime);</p>
<p>if (response!=null)<br />
System.out.println(&#8220;Message RECEIVED.&#8221;);<br />
else<br />
{<br />
System.out.println(&#8220;Message TIMEOUT.&#8221;);<br />
return null;<br />
}</p>
<p>String sendIDString = objmessage.getJMSMessageID();<br />
String respIDString = response.getJMSCorrelationID();<br />
if (!sendIDString.equals(respIDString))<br />
{<br />
String msg = &#8220;&#8216;&#8221;+sendIDString+&#8221;&#8216;!=&#8217;&#8221;+respIDString+&#8221;&#8216;&#8221;;<br />
System.out.println(&#8220;JMSCorrelationID MISMATCH (&#8220;+msg+&#8221;).&#8221;);<br />
return null;<br />
}</p>
<p>return response;<br />
}<br />
catch (Exception e)<br />
{<br />
System.out.println(&#8220;JMSException &#8216;&#8221;+e.getMessage()+&#8221;&#8216;&#8221;);<br />
return null;<br />
}<br />
finally<br />
{<br />
try<br />
{<br />
if (qReceiver!=null) qReceiver.close();<br />
if (qSender!=null) qSender.close();<br />
if (tempQueue!=null) tempQueue.delete();<br />
if (queueConnection!=null) queueConnection.close();<br />
}<br />
catch (Exception e){;}<br />
}<br />
}</p>
<p>public static Queue getQueue(String name, QueueSession session)<br />
{<br />
try<br />
{<br />
return session.createQueue(name);<br />
}<br />
catch(JMSException e)<br />
{<br />
throw new RuntimeException(&#8220;Session closed&#8221;);<br />
}<br />
}<br />
}</p>
<h2>Conclusione</h2>
<p>Come si può vedere il problema è facilmente aggirabile,                         basta solo fare un po&#8217; di googling e cercare nei javadoc.</p>
<p><a href="mailto:ottuzzi@gmail.com"><img src="http://localhost/wordpress/wp-content/uploads/2008/02/ottuzziemail.png" alt="ottuzziemail Implementazione di un QueueRequestor con timeout."  title="Implementazione di un QueueRequestor con timeout." /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.brucalipto.org/java/implementazione-di-un-queuerequestor-con-timeout/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

