AMQ-6463 - provide connection to scheduler context in case of deferred send or error

This commit is contained in:
gtully 2016-10-11 11:58:37 +01:00
parent 1ac89543a8
commit 74a5381b9a
2 changed files with 263 additions and 0 deletions

View File

@ -24,13 +24,20 @@ import org.apache.activemq.advisory.AdvisorySupport;
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerFilter;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.Connection;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.Connector;
import org.apache.activemq.broker.ProducerBrokerExchange;
import org.apache.activemq.broker.region.ConnectionStatistics;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.Command;
import org.apache.activemq.command.ConnectionControl;
import org.apache.activemq.command.ExceptionResponse;
import org.apache.activemq.command.Message;
import org.apache.activemq.command.MessageId;
import org.apache.activemq.command.ProducerId;
import org.apache.activemq.command.ProducerInfo;
import org.apache.activemq.command.Response;
import org.apache.activemq.openwire.OpenWireFormat;
import org.apache.activemq.security.SecurityContext;
import org.apache.activemq.state.ProducerState;
@ -64,6 +71,116 @@ public class SchedulerBroker extends BrokerFilter implements JobListener {
this.store = store;
this.producerId.setConnectionId(ID_GENERATOR.generateId());
this.context.setSecurityContext(SecurityContext.BROKER_SECURITY_CONTEXT);
// we only get response on unexpected error
this.context.setConnection(new Connection() {
@Override
public Connector getConnector() {
return null;
}
@Override
public void dispatchSync(Command message) {
if (message instanceof ExceptionResponse) {
LOG.warn("Unexpected response: " + message);
}
}
@Override
public void dispatchAsync(Command command) {
if (command instanceof ExceptionResponse) {
LOG.warn("Unexpected response: " + command);
}
}
@Override
public Response service(Command command) {
return null;
}
@Override
public void serviceException(Throwable error) {
LOG.warn("Unexpected exception: " + error, error);
}
@Override
public boolean isSlow() {
return false;
}
@Override
public boolean isBlocked() {
return false;
}
@Override
public boolean isConnected() {
return false;
}
@Override
public boolean isActive() {
return false;
}
@Override
public int getDispatchQueueSize() {
return 0;
}
@Override
public ConnectionStatistics getStatistics() {
return null;
}
@Override
public boolean isManageable() {
return false;
}
@Override
public String getRemoteAddress() {
return null;
}
@Override
public void serviceExceptionAsync(IOException e) {
LOG.warn("Unexpected async ioexception: " + e, e);
}
@Override
public String getConnectionId() {
return null;
}
@Override
public boolean isNetworkConnection() {
return false;
}
@Override
public boolean isFaultTolerantConnection() {
return false;
}
@Override
public void updateClient(ConnectionControl control) {}
@Override
public int getActiveTransactionCount() {
return 0;
}
@Override
public Long getOldestActiveTransactionDuration() {
return null;
}
@Override
public void start() throws Exception {}
@Override
public void stop() throws Exception {}
});
this.context.setBroker(next);
this.systemUsage = brokerService.getSystemUsage();

View File

@ -0,0 +1,146 @@
/**
* 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.bugs;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.JmsTestSupport;
import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.broker.region.policy.VMPendingQueueMessageStoragePolicy;
import org.apache.activemq.broker.region.policy.VMPendingSubscriberMessageStoragePolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.util.DefaultTestAppender;
import org.apache.activemq.util.Wait;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class AMQ6463Test extends JmsTestSupport {
static final Logger LOG = LoggerFactory.getLogger(AMQ6463Test.class);
ActiveMQQueue queueA = new ActiveMQQueue("QUEUE.A");
protected TransportConnector connector;
protected ActiveMQConnection connection;
protected DefaultTestAppender appender;
final AtomicInteger errors = new AtomicInteger(0);
final AtomicBoolean gotUsageBlocked = new AtomicBoolean();
public void testBlockedSechedulerSendNoError() throws Exception {
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory();
connection = (ActiveMQConnection)factory.createConnection();
connections.add(connection);
connection.start();
final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
final MessageProducer producer = session.createProducer(queueA);
TextMessage message = session.createTextMessage("test msg");
final int numMessages = 20;
long time = 5;
message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "* * * * *");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 5);
message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, numMessages - 1);
producer.send(message);
producer.close();
// lets not consume till producer is blocked
Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return gotUsageBlocked.get();
}
});
MessageConsumer consumer = session.createConsumer(queueA);
TextMessage msg;
for (int idx = 0; idx < numMessages; ++idx) {
msg = (TextMessage) consumer.receive(10000);
assertNotNull("received: " + idx, msg);
msg.acknowledge();
}
assertTrue("no errors in the log", errors.get() == 0);
assertTrue("got blocked message", gotUsageBlocked.get());
}
protected BrokerService createBroker() throws Exception {
BrokerService service = new BrokerService();
service.setPersistent(true);
service.setUseJmx(false);
service.setSchedulerSupport(true);
// Setup a destination policy where it takes only 1 message at a time.
PolicyMap policyMap = new PolicyMap();
PolicyEntry policy = new PolicyEntry();
policy.setMemoryLimit(1);
policy.setPendingSubscriberPolicy(new VMPendingSubscriberMessageStoragePolicy());
policy.setPendingQueuePolicy(new VMPendingQueueMessageStoragePolicy());
policy.setProducerFlowControl(true);
policyMap.setDefaultEntry(policy);
service.setDestinationPolicy(policyMap);
connector = service.addConnector("tcp://localhost:0");
return service;
}
public void setUp() throws Exception {
setAutoFail(true);
DefaultTestAppender appender = new DefaultTestAppender() {
@Override
public void doAppend(LoggingEvent event) {
if (event.getLevel().equals(Level.ERROR)) {
errors.incrementAndGet();
} else if (event.getLevel().equals(Level.INFO) && event.getRenderedMessage().contains("Usage Manager Memory Limit")) {
gotUsageBlocked.set(true);
}
}
};
org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
rootLogger.addAppender(appender);
super.setUp();
}
protected void tearDown() throws Exception {
org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
rootLogger.removeAppender(appender);
if (connection != null) {
connection.close();
}
super.tearDown();
}
protected ConnectionFactory createConnectionFactory() throws Exception {
return new ActiveMQConnectionFactory(connector.getConnectUri());
}
}