<!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">JBoss 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;">
Re: Redelivery problem when using MessageSelectors
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/theamazingtoby">Toby Morris</a> in <i>JBoss Messaging</i> - <a href="http://community.jboss.org/message/574516#574516">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Here's a simple unit test that performs two tests. Both tests send two messages and attempt to receive two messages. The first sends messages with different custom property "SenderID" values and the second sends messages with the same custom property "SenderID" values. The first succeeeds and the second fails. The JMS queue has a redelivery value greater than 0.</p><blockquote class="jive-quote"><p>package jmstest;<br/><br/>import static org.junit.Assert.assertTrue;<br/>import java.io.Serializable;<br/>import java.util.Hashtable;<br/>import java.util.Map;<br/>import java.util.Properties;<br/>import javax.jms.DeliveryMode;<br/>import javax.jms.JMSException;<br/>import javax.jms.MessageProducer;<br/>import javax.jms.ObjectMessage;<br/>import javax.jms.Queue;<br/>import javax.jms.QueueConnection;<br/>import javax.jms.QueueConnectionFactory;<br/>import javax.jms.QueueReceiver;<br/>import javax.jms.QueueSession;<br/>import javax.naming.Context;<br/>import javax.naming.InitialContext;<br/>import javax.naming.NamingException;<br/>import org.apache.log4j.Logger;<br/>import org.junit.After;<br/>import org.junit.AfterClass;<br/>import org.junit.Before;<br/>import org.junit.BeforeClass;<br/>import org.junit.Test;<br/><br/>public class JMSQueueTest<br/>{<br/>    private static Logger log = Logger.getLogger(JMSQueueTest.class);<br/><br/>    private String jmsServer = System.getProperty("JMSSERVER");<br/><br/>    private String jmsQueueName = System.getProperty("QUEUE");<br/><br/>    @BeforeClass<br/>    public static void setUpBeforeClass() throws Exception<br/>    {<br/>    }<br/><br/>    @AfterClass<br/>    public static void tearDownAfterClass() throws Exception<br/>    {<br/>    }<br/><br/>    @Before<br/>    public void setUp() throws Exception<br/>    {<br/>        log.info("setUp - cleaning up");<br/>        clearQueue();<br/>    }<br/><br/>    @After<br/>    public void tearDown() throws Exception<br/>    {<br/>        log.info("tearDown - cleaning up");<br/>        clearQueue();<br/>    }<br/><br/>    public void clearQueue() throws Exception<br/>    {<br/>        boolean areThereMore = true;<br/>        while (areThereMore)<br/>        {<br/>            try<br/>            {<br/>                ObjectMessage message = null;<br/>                message = getNextMessage(jmsServer, jmsQueueName, false, 1, null);<br/>                if (message != null && message.getObject() != null)<br/>                {<br/>                    log.debug("There's more");<br/>                }<br/>                else<br/>                {<br/>                    log.debug("No more.");<br/>                    areThereMore = false;<br/>                }<br/>            }<br/>            catch (Exception e)<br/>            {<br/>                log.error(e.getMessage(), e);<br/>            }<br/>        }<br/>    }<br/><br/>    @Test<br/>    public void testJMSQueueSucceeds() throws Exception<br/>    {<br/>        ObjectMessage om = null;<br/>        try<br/>        {<br/>            // /////////////////////////////////////////////////////////////////////////<br/>            // Send a couple of messages<br/>            // /////////////////////////////////////////////////////////////////////////<br/>            log.info("--------------------------------------------------------------------------------");<br/>            log.info("sending two messages");<br/>            this.sendJMSMessage("test1");<br/>            this.sendJMSMessage("test2");<br/>            log.info("--------------------------------------------------------------------------------");<br/>            log.debug("Manually retrieving messages");<br/>            om = this.getNextMessage(jmsServer, jmsQueueName, false, 1, "senderID = 'test1'");<br/>            assertTrue(om != null);<br/>            om = this.getNextMessage(jmsServer, jmsQueueName, false, 1, "senderID = 'test2'");<br/>            assertTrue(om != null);<br/>        }<br/>        catch (Exception e)<br/>        {<br/>            log.error(e.getMessage(), e);<br/>        }<br/>        finally<br/>        {<br/>        }<br/>        log.debug("Done.");<br/>    }<br/><br/>    @Test<br/>    public void testJMSQueueFails() throws Exception<br/>    {<br/>        ObjectMessage om = null;<br/>        try<br/>        {<br/>            // /////////////////////////////////////////////////////////////////////////<br/>            // Send a couple of messages<br/>            // /////////////////////////////////////////////////////////////////////////<br/>            log.info("--------------------------------------------------------------------------------");<br/>            log.info("sending two messages");<br/>            this.sendJMSMessage("test1");<br/>            this.sendJMSMessage("test1");<br/>            log.info("--------------------------------------------------------------------------------");<br/>            log.debug("Manually retrieving messages");<br/>            om = this.getNextMessage(jmsServer, jmsQueueName, false, 1, "senderID = 'test1'");<br/>            assertTrue(om != null);<br/>            om = this.getNextMessage(jmsServer, jmsQueueName, false, 1, "senderID = 'test1'");<br/>            assertTrue(om != null);<br/>        }<br/>        catch (Exception e)<br/>        {<br/>            log.error(e.getMessage(), e);<br/>        }<br/>        finally<br/>        {<br/>        }<br/>        log.debug("Done.");<br/>    }<br/><br/>    private void sendJMSMessage(String senderID) throws Exception<br/>    {<br/>        try<br/>        {<br/>            log.debug("Sending...");<br/>            String message = "This is a test";<br/>            Hashtable<String, Object> properties = new Hashtable<String, Object>();<br/>            if (senderID != null)<br/>            {<br/>                properties.put("senderID", senderID);<br/>                log.debug("senderID = " + senderID);<br/>            }<br/>            this.sendMessageX(jmsServer, jmsQueueName, true, (Serializable) message, 20, null, 0, properties, DeliveryMode.NON_PERSISTENT, 4);<br/>            log.debug("Message sent");<br/>        }<br/>        catch (Exception e)<br/>        {<br/>            log.error(e.getMessage(), e);<br/>        }<br/>    }<br/><br/>    private void sendMessageX(String jmsServer, String jmsQueueName, boolean clientAcknowledge, Serializable message, int ttl, JMSQueue replyTo, int replyWaitSeconds, Map<String, Object> properties, int persistence, int priority) throws NamingException, JMSException<br/>    {<br/>        InitialContext ctx = null;<br/>        QueueConnection conn = null;<br/>        QueueConnectionFactory tcf = null;<br/>        Queue queue = null;<br/>        QueueSession session = null;<br/>        MessageProducer producer = null;<br/>        try<br/>        {<br/>            Properties ctxProperties = new Properties();<br/>            ctxProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");<br/>            ctxProperties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");<br/>            ctxProperties.put(Context.PROVIDER_URL, jmsServer);<br/>            ctx = new InitialContext(ctxProperties);<br/>            tcf = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");<br/>            conn = tcf.createQueueConnection();<br/>            queue = (Queue) ctx.lookup(jmsQueueName);<br/>            conn.start();<br/>            if (clientAcknowledge)<br/>            {<br/>                session = conn.createQueueSession(false, QueueSession.CLIENT_ACKNOWLEDGE);<br/>            }<br/>            else<br/>            {<br/>                session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);<br/>            }<br/>            // ////////////////////////////<br/>            log.debug("Start sendMessage()");<br/>            producer = session.createProducer(queue);<br/>            ObjectMessage objMessage = session.createObjectMessage(message);<br/>            if (properties != null)<br/>            {<br/>                for (String key : properties.keySet())<br/>                {<br/>                    objMessage.setObjectProperty(key, properties.get(key));<br/>                }<br/>            }<br/>            if (replyTo != null)<br/>            {<br/>                objMessage.setJMSReplyTo(replyTo.getJMSQueue());<br/>                objMessage.setStringProperty("replyToServer", replyTo.getJmsServer());<br/>                objMessage.setStringProperty("replyToQueue", replyTo.getJmsQueueName());<br/>            }<br/>            producer.setTimeToLive(ttl * 1000);<br/>            log.debug("-------------Sending Message");<br/>            producer.send(objMessage, persistence, priority, 0);<br/>            producer.close();<br/>            objMessage = null;<br/>            // //////////////////////////////<br/>        }<br/>        finally<br/>        {<br/>            try<br/>            {<br/>                conn.close();<br/>                conn = null;<br/>            }<br/>            catch (Exception e)<br/>            {<br/>                log.error(e.getMessage(), e);<br/>            }<br/>            try<br/>            {<br/>                session.close();<br/>                session = null;<br/>            }<br/>            catch (Exception e)<br/>            {<br/>                log.error(e.getMessage(), e);<br/>            }<br/>        }<br/>    }<br/><br/>    private ObjectMessage getNextMessage(String jmsServer, String jmsQueueName, boolean clientAcknowledge, int queueWaitSeconds, String selector) throws NamingException, JMSException<br/>    {<br/>        InitialContext ctx = null;<br/>        QueueConnection conn = null;<br/>        QueueConnectionFactory tcf = null;<br/>        Queue queue = null;<br/>        QueueSession session = null;<br/>        QueueReceiver consumer = null;<br/>        try<br/>        {<br/>            Properties properties = new Properties();<br/>            properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");<br/>            properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");<br/>            properties.put(Context.PROVIDER_URL, jmsServer);<br/>            ctx = new InitialContext(properties);<br/>            tcf = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");<br/>            conn = tcf.createQueueConnection();<br/>            queue = (Queue) ctx.lookup(jmsQueueName);<br/>            conn.start();<br/>            if (clientAcknowledge)<br/>            {<br/>                log.debug("CLIENT_ACKNOWLEDGE");<br/>                session = conn.createQueueSession(false, QueueSession.CLIENT_ACKNOWLEDGE);<br/>            }<br/>            else<br/>            {<br/>                log.debug("AUTO_ACKNOWLEDGE");<br/>                session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);<br/>            }<br/>            // ////////////////////////////<br/>            log.debug("Start getNextMessage()");<br/>            if (selector == null)<br/>            {<br/>                log.debug("Not using a selector");<br/>                consumer = session.createReceiver(queue);<br/>            }<br/>            else<br/>            {<br/>                log.debug("Using a selector");<br/>                consumer = session.createReceiver(queue, selector);<br/>            }<br/>            ObjectMessage message = (ObjectMessage) consumer.receive(queueWaitSeconds * 1000);<br/>            consumer.close();<br/>            if (message != null)<br/>            {<br/>                log.debug("----------Got a message");<br/>                return message;<br/>            }<br/>            else<br/>            {<br/>                log.debug("---No message found in " + queueWaitSeconds + " seconds");<br/>                return null;<br/>            }<br/>            // ////////////////////////////////<br/>        }<br/>        finally<br/>        {<br/>            try<br/>            {<br/>                conn.close();<br/>                conn = null;<br/>            }<br/>            catch (Exception e)<br/>            {<br/>                log.error(e.getMessage(), e);<br/>            }<br/>            try<br/>            {<br/>                session.close();<br/>                session = null;<br/>            }<br/>            catch (Exception e)<br/>            {<br/>                log.error(e.getMessage(), e);<br/>            }<br/>        }<br/>    }<br/>}</p></blockquote></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/574516#574516">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>