Update the receive counter on reads to avoid dropping connections
wrongly.
This commit is contained in:
Timothy Bish 2015-06-03 09:54:02 -04:00
parent 9810e61b1b
commit 848adc4b5d
2 changed files with 93 additions and 4 deletions

View File

@ -29,7 +29,6 @@ import java.nio.channels.SocketChannel;
import javax.net.SocketFactory; import javax.net.SocketFactory;
import org.apache.activemq.command.Command;
import org.apache.activemq.openwire.OpenWireFormat; import org.apache.activemq.openwire.OpenWireFormat;
import org.apache.activemq.transport.Transport; import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.tcp.TcpTransport; import org.apache.activemq.transport.tcp.TcpTransport;
@ -39,8 +38,8 @@ import org.apache.activemq.wireformat.WireFormat;
/** /**
* An implementation of the {@link Transport} interface using raw tcp/ip * An implementation of the {@link Transport} interface using raw tcp/ip
* *
* *
*/ */
public class NIOTransport extends TcpTransport { public class NIOTransport extends TcpTransport {
@ -59,16 +58,19 @@ public class NIOTransport extends TcpTransport {
super(wireFormat, socket); super(wireFormat, socket);
} }
@Override
protected void initializeStreams() throws IOException { protected void initializeStreams() throws IOException {
channel = socket.getChannel(); channel = socket.getChannel();
channel.configureBlocking(false); channel.configureBlocking(false);
// listen for events telling us when the socket is readable. // listen for events telling us when the socket is readable.
selection = SelectorManager.getInstance().register(channel, new SelectorManager.Listener() { selection = SelectorManager.getInstance().register(channel, new SelectorManager.Listener() {
@Override
public void onSelect(SelectorSelection selection) { public void onSelect(SelectorSelection selection) {
serviceRead(); serviceRead();
} }
@Override
public void onError(SelectorSelection selection, Throwable error) { public void onError(SelectorSelection selection, Throwable error) {
if (error instanceof IOException) { if (error instanceof IOException) {
onException((IOException)error); onException((IOException)error);
@ -103,6 +105,8 @@ public class NIOTransport extends TcpTransport {
break; break;
} }
this.receiveCounter += readSize;
if (currentBuffer.hasRemaining()) { if (currentBuffer.hasRemaining()) {
continue; continue;
} }
@ -135,7 +139,7 @@ public class NIOTransport extends TcpTransport {
currentBuffer.flip(); currentBuffer.flip();
Object command = wireFormat.unmarshal(new DataInputStream(new NIOInputStream(currentBuffer))); Object command = wireFormat.unmarshal(new DataInputStream(new NIOInputStream(currentBuffer)));
doConsume((Command)command); doConsume(command);
nextFrameSize = -1; nextFrameSize = -1;
inputBuffer.clear(); inputBuffer.clear();
@ -152,12 +156,14 @@ public class NIOTransport extends TcpTransport {
} }
} }
@Override
protected void doStart() throws Exception { protected void doStart() throws Exception {
connect(); connect();
selection.setInterestOps(SelectionKey.OP_READ); selection.setInterestOps(SelectionKey.OP_READ);
selection.enable(); selection.enable();
} }
@Override
protected void doStop(ServiceStopper stopper) throws Exception { protected void doStop(ServiceStopper stopper) throws Exception {
if (selection != null) { if (selection != null) {
selection.close(); selection.close();

View File

@ -0,0 +1,83 @@
/**
* 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 javax.jms.BytesMessage;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AMQ6000Test {
private static final Logger LOG = LoggerFactory.getLogger(AMQ6000Test.class);
private BrokerService brokerService;
private String connectionUri;
@Before
public void setUp() throws Exception {
brokerService = new BrokerService();
brokerService.setPersistent(false);
brokerService.setUseJmx(false);
brokerService.addConnector("nio://0.0.0.0:0?"
+ "wireFormat.maxInactivityDurationInitalDelay=1000&wireFormat.maxInactivityDuration=300");
brokerService.start();
connectionUri = brokerService.getTransportConnectorByScheme("nio").getPublishableConnectString();
}
@After
public void tearDown() throws Exception {
brokerService.stop();
brokerService.waitUntilStopped();
}
@Test
public void testReadCounter() throws Exception {
LOG.info("Connecting to: {}", connectionUri);
byte[] payload = new byte[50 * 1024 * 1024];
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri + "?useInactivityMonitor=false");
final ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
connection.start();
LOG.info("Connected to: {}", connection.getTransport());
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("test");
MessageProducer producer = session.createProducer(queue);
BytesMessage message = session.createBytesMessage();
message.writeBytes(payload);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
producer.send(message);
connection.close();
}
}