|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.activemq.broker.virtual; |
| 18 | + |
| 19 | +import jakarta.jms.Destination; |
| 20 | +import jakarta.jms.JMSException; |
| 21 | +import jakarta.jms.MessageConsumer; |
| 22 | +import jakarta.jms.MessageProducer; |
| 23 | +import jakarta.jms.Session; |
| 24 | +import org.apache.activemq.broker.Broker; |
| 25 | +import org.apache.activemq.broker.BrokerService; |
| 26 | +import org.apache.activemq.broker.ConnectionContext; |
| 27 | +import org.apache.activemq.broker.region.DestinationFilter; |
| 28 | +import org.apache.activemq.broker.region.DestinationInterceptor; |
| 29 | +import org.apache.activemq.broker.region.virtual.VirtualDestination; |
| 30 | +import org.apache.activemq.broker.region.virtual.VirtualDestinationInterceptor; |
| 31 | +import org.apache.activemq.broker.region.virtual.VirtualTopic; |
| 32 | +import org.apache.activemq.command.ActiveMQDestination; |
| 33 | +import org.apache.activemq.command.ActiveMQQueue; |
| 34 | +import org.apache.activemq.command.ActiveMQTopic; |
| 35 | +import org.apache.activemq.spring.ConsumerBean; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +public class VirtualTopicSelectorWithAnotherInterceptorTest extends CompositeTopicTest { |
| 40 | + |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(VirtualTopicSelectorWithAnotherInterceptorTest.class); |
| 42 | + |
| 43 | + protected Destination getConsumer1Dsetination() { |
| 44 | + return new ActiveMQQueue("Consumer.1.VirtualTopic.TEST"); |
| 45 | + } |
| 46 | + |
| 47 | + protected Destination getConsumer2Dsetination() { |
| 48 | + return new ActiveMQQueue("Consumer.2.VirtualTopic.TEST"); |
| 49 | + } |
| 50 | + |
| 51 | + protected Destination getProducerDestination() { |
| 52 | + return new ActiveMQTopic("VirtualTopic.TEST"); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void assertMessagesArrived(ConsumerBean messageList1, ConsumerBean messageList2) { |
| 57 | + messageList1.assertMessagesArrived(total / 2); |
| 58 | + messageList2.assertMessagesArrived(total / 2); |
| 59 | + |
| 60 | + messageList1.flushMessages(); |
| 61 | + messageList2.flushMessages(); |
| 62 | + |
| 63 | + LOG.info("validate no other messages on queues"); |
| 64 | + try { |
| 65 | + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 66 | + |
| 67 | + Destination destination1 = getConsumer1Dsetination(); |
| 68 | + Destination destination2 = getConsumer2Dsetination(); |
| 69 | + MessageConsumer c1 = session.createConsumer(destination1, null); |
| 70 | + MessageConsumer c2 = session.createConsumer(destination2, null); |
| 71 | + c1.setMessageListener(messageList1); |
| 72 | + c2.setMessageListener(messageList2); |
| 73 | + |
| 74 | + |
| 75 | + LOG.info("send one simple message that should go to both consumers"); |
| 76 | + MessageProducer producer = session.createProducer(getProducerDestination()); |
| 77 | + assertNotNull(producer); |
| 78 | + |
| 79 | + producer.send(session.createTextMessage("Last Message")); |
| 80 | + |
| 81 | + messageList1.assertMessagesArrived(1); |
| 82 | + messageList2.assertMessagesArrived(1); |
| 83 | + |
| 84 | + } catch (JMSException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + fail("unexpeced ex while waiting for last messages: " + e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected BrokerService createBroker() throws Exception { |
| 92 | + // use message selectors on consumers that need to propagate up to the virtual |
| 93 | + // topic dispatch so that un matched messages do not linger on subscription queues |
| 94 | + messageSelector1 = "odd = 'yes'"; |
| 95 | + messageSelector2 = "odd = 'no'"; |
| 96 | + |
| 97 | + BrokerService broker = new BrokerService(); |
| 98 | + broker.setPersistent(false); |
| 99 | + |
| 100 | + VirtualTopic virtualTopic = new VirtualTopic(); |
| 101 | + // the new config that enables selectors on the interceptor |
| 102 | + virtualTopic.setSelectorAware(true); |
| 103 | + VirtualDestinationInterceptor interceptor = new VirtualDestinationInterceptor(); |
| 104 | + interceptor.setVirtualDestinations(new VirtualDestination[]{virtualTopic}); |
| 105 | + TestDestinationInterceptor testInterceptor = new TestDestinationInterceptor(); |
| 106 | + broker.setDestinationInterceptors(new DestinationInterceptor[]{testInterceptor, interceptor}); |
| 107 | + return broker; |
| 108 | + } |
| 109 | + |
| 110 | + private static class TestDestinationInterceptor implements DestinationInterceptor { |
| 111 | + |
| 112 | + @Override |
| 113 | + public org.apache.activemq.broker.region.Destination intercept(org.apache.activemq.broker.region.Destination destination) { |
| 114 | + return new DestinationFilter(destination); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void remove(org.apache.activemq.broker.region.Destination destination) { |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void create(Broker broker, ConnectionContext context, ActiveMQDestination destination) throws Exception { |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments