diff --git a/activemq-core/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java b/activemq-core/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java index 517e0eaa3d..7cca8cf589 100755 --- a/activemq-core/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java +++ b/activemq-core/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java @@ -162,7 +162,6 @@ public class ActiveMQMessageConsumer implements MessageAvailableConsumer, StatsC } this.session = session; - this.selector = selector; this.redeliveryPolicy = session.connection.getRedeliveryPolicy(); setTransformer(session.getTransformer()); @@ -174,6 +173,7 @@ public class ActiveMQMessageConsumer implements MessageAvailableConsumer, StatsC this.info.setNoLocal(noLocal); this.info.setDispatchAsync(dispatchAsync); this.info.setRetroactive(this.session.connection.isUseRetroactiveConsumer()); + this.info.setSelector(null); // Allows the options on the destination to configure the consumerInfo if (dest.getOptions() != null) { @@ -184,11 +184,16 @@ public class ActiveMQMessageConsumer implements MessageAvailableConsumer, StatsC this.info.setDestination(dest); this.info.setBrowser(browser); if (selector != null && selector.trim().length() != 0) { - // Validate that the selector + // Validate the selector new SelectorParser().parse(selector); this.info.setSelector(selector); + this.selector = selector; + } else if (info.getSelector() != null) { + // Validate the selector + new SelectorParser().parse(this.info.getSelector()); + this.selector = this.info.getSelector(); } else { - this.info.setSelector(null); + this.selector = null; } this.stats = new JMSConsumerStatsImpl(session.getSessionStats(), dest); diff --git a/activemq-core/src/test/java/org/apache/activemq/config/ConfigUsingDestinationOptions.java b/activemq-core/src/test/java/org/apache/activemq/config/ConfigUsingDestinationOptions.java new file mode 100644 index 0000000000..ffdc56ba9d --- /dev/null +++ b/activemq-core/src/test/java/org/apache/activemq/config/ConfigUsingDestinationOptions.java @@ -0,0 +1,72 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.config; + +import junit.framework.TestCase; +import org.apache.activemq.command.ActiveMQQueue; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.ActiveMQMessageConsumer; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.Session; +import javax.jms.InvalidSelectorException; + +public class ConfigUsingDestinationOptions extends TestCase { + public void testValidSelectorConfig() throws JMSException { + ActiveMQQueue queue = new ActiveMQQueue("TEST.FOO?consumer.selector=test=1"); + + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost"); + Connection conn = factory.createConnection(); + Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + + ActiveMQMessageConsumer cons; + // JMS selector should be priority + cons = (ActiveMQMessageConsumer) sess.createConsumer(queue, "test=2"); + assertEquals("test=2", cons.getMessageSelector()); + + // Test setting using JMS destinations + cons = (ActiveMQMessageConsumer) sess.createConsumer(queue); + assertEquals("test=1", cons.getMessageSelector()); + } + + public void testInvalidSelectorConfig() throws JMSException { + ActiveMQQueue queue = new ActiveMQQueue("TEST.FOO?consumer.selector=test||1"); + + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost"); + Connection conn = factory.createConnection(); + Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + + ActiveMQMessageConsumer cons; + // JMS selector should be priority + try { + cons = (ActiveMQMessageConsumer) sess.createConsumer(queue, "test||1"); + fail("Selector should be invalid"); + } catch (InvalidSelectorException e) { + + } + + // Test setting using JMS destinations + try { + cons = (ActiveMQMessageConsumer) sess.createConsumer(queue); + fail("Selector should be invalid"); + } catch (InvalidSelectorException e) { + + } + } +}