<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
JMS Queue/Topic Issue (JBoss SOA 4.3 - JBoss AS 4.2.2)
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/jbossuseruseruser">J H</a> in <i>JBoss Messaging</i> - <a href="http://community.jboss.org/message/549085#549085">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>I have tried this with both a Queue and Topic. Within the destinations-service.xml file, I have configured a new Queue or Topic, simply by copying and pasting the example one and changing the name (testTopic > newTopic). I have a class that sends simple text messages to the topic and then another class that receieves them. The receiving class is used within a very simple GUI to get the message and display it in a text area. This all works 'fine'.</p><p>The problem is after a certain amount of time (there is no regularity in the time this happens...) messages stop being received. Whilst using a topic I have seen the subscription in the JMX-Console and noticed that it is no longer there when messages are not being received. I have tried leaving sessions open - I've tried closing them and then making a new subscription every time - but no matter what I try it never seems to 'stay alive'. I then turned to Queues, thinking that the same problem couldn't possibly apply, but I was wrong, my GUI client stops receiving messages after an arbitrary amount of time. So my question is: Does JBoss have configuration settings for a subscription? Or something along those lines. Or is it something that I am doing wrong:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>destinations-service.xml</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><mbean code="org.jboss.jms.server.destination.TopicService"
      name="jboss.messaging.destination:service=Topic,name=newTopic"
      xmbean-dd="xmdesc/Topic-xmbean.xml"></span>
      <span class="jive-xml-tag"><depends optional-attribute-name="ServerPeer"></span>jboss.messaging:service=ServerPeer<span class="jive-xml-tag"></depends></span>
      <span class="jive-xml-tag"><depends></span>jboss.messaging:service=PostOffice<span class="jive-xml-tag"></depends></span>
      <span class="jive-xml-tag"><attribute name="SecurityConfig"></span>
         <span class="jive-xml-tag"><security></span>
            <span class="jive-xml-tag"><role name="guest" read="true" write="true"/></span>
            <span class="jive-xml-tag"><role name="publisher" read="true" write="true" create="true"/></span>
         <span class="jive-xml-tag"></security></span>
      <span class="jive-xml-tag"></attribute></span>
<span class="jive-xml-tag"></mbean></span> 
</code></pre><p><br/>TopicReceiver.java:</p><pre class="jive-pre"><code class="jive-code jive-java">    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_TOPIC_NAME = <font color="red">"topic/newTopic"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_JNDI_CONNECTION_FACTORY = <font color="red">"ConnectionFactory"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_JNDI_PACKAGE = <font color="red">"org.jboss.naming:org.jnp.interfaces"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_JNDI_DRIVER = <font color="red">"org.jnp.interfaces.NamingContextFactory"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_URL = <font color="red">"jnp://localhost:1099"</font>;
    <font color="darkgreen">//The topic to which we will be publishing messages</font>
    <font color="navy"><b>private</b></font> Topic testTopic;
    <font color="darkgreen">//The factory that will build us JMS connections</font>
    <font color="navy"><b>private</b></font> TopicConnectionFactory topicFactory;
    <font color="darkgreen">//The connection to the JMS server</font>
    <font color="navy"><b>private</b></font> TopicConnection connection;
    <font color="darkgreen">//The subscriber object</font>
    <font color="navy"><b>private</b></font> TopicSubscriber subscriber;
    <font color="darkgreen">//The session for receiving</font>
    <font color="navy"><b>private</b></font> TopicSession subscribeSession;
    <font color="navy"><b>private</b></font> String messageText = <font color="red">""</font>;
 
   
    <font color="darkgreen">/**
     * Calls the incomingMessage() method on instantiation
     * to get the message from the subscribed Topic.
     */</font>
    <font color="navy"><b>public</b></font> TopicReceiver() <font color="navy">{</font>
        this.setup();
    <font color="navy">}</font>
   
    <font color="darkgreen">/**
     * Gets the message that has been stored from the subscribed
     * topic.
     * @return The newest incoming message.
     */</font>
    <font color="navy"><b>public</b></font> String getMessage()<font color="navy">{</font>
        <font color="navy"><b>return</b></font> messageText;
    <font color="navy">}</font>
   
    <font color="darkgreen">/**
     * Sets up the connection to JBoss and the Subscriber, who will
     * listen for incoming messages.
     */</font>
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setup()<font color="navy">{</font>
        Properties prop = <font color="navy"><b>new</b></font> Properties();
           prop.put(<font color="red">"java.naming.factory.initial"</font>,JBOSS_JNDI_DRIVER);
           prop.put(<font color="red">"java.naming.provider.url"</font>,JBOSS_URL);
           prop.put(<font color="red">"java.naming.factory.url.pkgs"</font>,JBOSS_JNDI_PACKAGE);
           InitialContext context;
        <font color="navy"><b>try</b></font> <font color="navy">{</font>
        context = <font color="navy"><b>new</b></font> InitialContext(prop);
           <font color="darkgreen">//Lookup the JMS connection factory and topic</font>
           topicFactory = (TopicConnectionFactory)context.lookup(JBOSS_JNDI_CONNECTION_FACTORY);
           connection = topicFactory.createTopicConnection();
           testTopic = (Topic)context.lookup(JBOSS_TOPIC_NAME);
          
           subscribeSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
           subscriber = subscribeSession.createSubscriber(testTopic);
           connection.start();
 
        <font color="navy">}</font> <font color="navy"><b>catch</b></font> (NamingException e) <font color="navy">{</font>
            e.printStackTrace();
        <font color="navy">}</font> <font color="navy"><b>catch</b></font> (JMSException jms)<font color="navy">{</font>
            jms.printStackTrace();
        <font color="navy">}</font>
    <font color="navy">}</font>
 
    <font color="darkgreen">/**
     * Provides a user friendly view of the message - as a String.
     * @return The Message contained within the JMS Message object.
     */</font>
    <font color="navy"><b>public</b></font> String receiveMessage()<font color="navy">{</font>
        String msg = <font color="red">""</font>;
        <font color="navy"><b>try</b></font> <font color="navy">{</font>
            Message message = subscriber.receive();
            msg = ((TextMessage)message).getText();
            System.out.println(msg);
            <font color="darkgreen">//stop();</font>
        <font color="navy">}</font> <font color="navy"><b>catch</b></font> (JMSException e) <font color="navy">{</font>
            e.printStackTrace();
        <font color="navy">}</font>
        <font color="navy"><b>return</b></font> msg;
    <font color="navy">}</font>
   
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> stop()<font color="navy">{</font>
        <font color="navy"><b>try</b></font> <font color="navy">{</font>
            subscribeSession.close();
            subscriber.close();
            connection.stop();
            connection.close();
        <font color="navy">}</font> <font color="navy"><b>catch</b></font> (JMSException e) <font color="navy">{</font>
            e.printStackTrace();
        <font color="navy">}</font>
       
    <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>TopicSender.java:</p><pre ___default_attr="java" jivemacro="code"><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_TOPIC_NAME = <font color="red">"topic/newTopic"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_JNDI_CONNECTION_FACTORY = <font color="red">"ConnectionFactory"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_JNDI_PACKAGE = <font color="red">"org.jboss.naming:org.jnp.interfaces"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_JNDI_DRIVER = <font color="red">"org.jnp.interfaces.NamingContextFactory"</font>;
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String JBOSS_URL = <font color="red">"jnp://localhost:1099"</font>;
    <font color="darkgreen">//The topic to which we will be publishing messages</font>
    <font color="navy"><b>private</b></font> Topic jbossTopic;
    <font color="darkgreen">//The factory that will build us JMS connections</font>
    <font color="navy"><b>private</b></font> TopicConnectionFactory topicFactory;
    <font color="darkgreen">//The connection to the JMS server</font>
    <font color="navy"><b>private</b></font> TopicConnection connection;
    <font color="darkgreen">//The publisher object</font>
    <font color="navy"><b>private</b></font> TopicPublisher publisher;
    <font color="darkgreen">//The session for sending</font>
    <font color="navy"><b>private</b></font> TopicSession publishSession;
   
    <font color="darkgreen">/**
     * Default Constructor
     */</font>
    <font color="navy"><b>public</b></font> TopicSender()<font color="navy">{</font>
 
 
 
 
    <font color="navy">}</font>
   
    <font color="darkgreen">/**
     * Prepares the connection to the Topic on the Server.
     */</font>
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> setup()<font color="navy">{</font>
        <font color="navy"><b>try</b></font><font color="navy">{</font>
                 Properties prop = <font color="navy"><b>new</b></font> Properties();
                prop.put(<font color="red">"java.naming.factory.initial"</font>,JBOSS_JNDI_DRIVER);
                prop.put(<font color="red">"java.naming.provider.url"</font>,JBOSS_URL);
                prop.put(<font color="red">"java.naming.factory.url.pkgs"</font>,JBOSS_JNDI_PACKAGE);
                InitialContext context = <font color="navy"><b>new</b></font> InitialContext(prop);
                <font color="darkgreen">//Lookup the JMS connection factory and topic</font>
                topicFactory = (TopicConnectionFactory)context.lookup(JBOSS_JNDI_CONNECTION_FACTORY);
                connection = topicFactory.createTopicConnection();
                jbossTopic = (Topic)context.lookup(JBOSS_TOPIC_NAME);
                <font color="darkgreen">//to send messages we create a session and a publisher</font>
                publishSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
                connection.start();
                <font color="navy">}</font>
                <font color="navy"><b>catch</b></font>(Exception e)<font color="navy">{</font>
                    e.printStackTrace();
                <font color="navy">}</font>
    <font color="navy">}</font>
   
    <font color="darkgreen">/**
     * Sends the message passed through the parameter to a topic named
     * 'topic/testTopic'
     * @param text - The message that is to be published to the topic.
     */</font>
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> sendMessage(String text)<font color="navy">{</font>
       <font color="navy"><b>try</b></font> <font color="navy">{</font> 
           this.setup();
               publisher = publishSession.createPublisher(jbossTopic);
               TextMessage tm = publishSession.createTextMessage(text);
               publisher.publish(tm);
               publisher.close();
               this.stop();
       <font color="navy">}</font>
       <font color="navy"><b>catch</b></font> (Exception e) <font color="navy">{</font> 
           e.printStackTrace();
       <font color="navy">}</font>
    <font color="navy">}</font>
 
 
    <font color="darkgreen">/**
     * Closes the connection and resources in use.
     */</font>
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> stop()<font color="navy">{</font>
        <font color="navy"><b>try</b></font> <font color="navy">{</font>
            connection.stop();
            publishSession.close();
            connection.close();
        <font color="navy">}</font> <font color="navy"><b>catch</b></font> (JMSException e) <font color="navy">{</font>
            e.printStackTrace();
        <font color="navy">}</font>
       
    <font color="navy">}</font>
</code></pre>
<p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p>
<p><span style="font-family: arial,helvetica,sans-serif;">Please let me know if you require more information - any feedback is appreciated,<br/><br/>Thanks,<br/><br/>J</span></p>
</pre></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/549085#549085">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss Messaging at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2042">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>