use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.
the class AmqpSendReceiveInterceptorTest method testCreateQueueReceiver.
@Test(timeout = 60000)
public void testCreateQueueReceiver() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
server.getRemotingService().addIncomingInterceptor(new AmqpInterceptor() {
@Override
public boolean intercept(AMQPMessage message, RemotingConnection connection) throws ActiveMQException {
latch.countDown();
return true;
}
});
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getTestName());
AmqpMessage message = new AmqpMessage();
message.setMessageId("msg" + 1);
message.setText("Test-Message");
sender.send(message);
assertTrue(latch.await(5, TimeUnit.SECONDS));
final CountDownLatch latch2 = new CountDownLatch(1);
server.getRemotingService().addOutgoingInterceptor(new AmqpInterceptor() {
@Override
public boolean intercept(AMQPMessage packet, RemotingConnection connection) throws ActiveMQException {
latch2.countDown();
return true;
}
});
AmqpReceiver receiver = session.createReceiver(getTestName());
receiver.flow(2);
AmqpMessage amqpMessage = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(amqpMessage);
assertEquals(latch2.getCount(), 0);
sender.close();
receiver.close();
connection.close();
}
use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.
the class AmqpSendReceiveInterceptorTest method testCheckInterceptedMessageProperties.
@Test(timeout = 60000)
public void testCheckInterceptedMessageProperties() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final String addressQueue = getTestName();
final String messageId = "lala200";
final String correlationId = "lala-corrId";
final String msgText = "Test intercepted message";
final boolean durableMsg = false;
final short priority = 8;
final long timeToLive = 10000;
final String replyTo = "reply-to-myQueue";
Map<String, Object> expectedProperties = new HashMap<>();
expectedProperties.put(ADDRESS, addressQueue);
expectedProperties.put(MESSAGE_ID, messageId);
expectedProperties.put(CORRELATION_ID, correlationId);
expectedProperties.put(MESSAGE_TEXT, msgText);
expectedProperties.put(DURABLE, durableMsg);
expectedProperties.put(PRIORITY, priority);
expectedProperties.put(REPLY_TO, replyTo);
expectedProperties.put(TIME_TO_LIVE, timeToLive);
server.getRemotingService().addIncomingInterceptor(new AmqpInterceptor() {
@Override
public boolean intercept(AMQPMessage message, RemotingConnection connection) throws ActiveMQException {
latch.countDown();
return checkMessageProperties(message, expectedProperties);
}
});
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getTestName());
AmqpMessage message = new AmqpMessage();
message.setMessageId(messageId);
message.setCorrelationId(correlationId);
message.setText(msgText);
message.setDurable(durableMsg);
message.setPriority(priority);
message.setReplyToAddress(replyTo);
message.setTimeToLive(timeToLive);
sender.send(message);
assertTrue(latch.await(2, TimeUnit.SECONDS));
final CountDownLatch latch2 = new CountDownLatch(1);
server.getRemotingService().addOutgoingInterceptor(new AmqpInterceptor() {
@Override
public boolean intercept(AMQPMessage packet, RemotingConnection connection) throws ActiveMQException {
latch2.countDown();
return checkMessageProperties(packet, expectedProperties);
}
});
AmqpReceiver receiver = session.createReceiver(getTestName());
receiver.flow(2);
AmqpMessage amqpMessage = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(amqpMessage);
assertEquals(latch2.getCount(), 0);
sender.close();
receiver.close();
connection.close();
}
use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.
the class AmqpOutboundConnectionTest method runOutboundConnectionTest.
private void runOutboundConnectionTest(boolean withSecurity, boolean closeFromClient) throws Exception {
final ActiveMQServer remote;
try {
securityEnabled = withSecurity;
remote = createServer(AMQP_PORT + 1);
} finally {
securityEnabled = false;
}
Wait.assertTrue(remote::isActive);
final Map<String, Object> config = new LinkedHashMap<>();
config.put(TransportConstants.HOST_PROP_NAME, "localhost");
config.put(TransportConstants.PORT_PROP_NAME, String.valueOf(AMQP_PORT + 1));
final ClientSASLFactory clientSASLFactory;
if (withSecurity) {
clientSASLFactory = availableMechanims -> {
if (availableMechanims != null && Arrays.asList(availableMechanims).contains("PLAIN")) {
return new PlainSASLMechanism(fullUser, fullPass);
} else {
return null;
}
};
} else {
clientSASLFactory = null;
}
final AtomicBoolean connectionOpened = new AtomicBoolean();
EventHandler eventHandler = new EventHandler() {
@Override
public void onRemoteOpen(Connection connection) throws Exception {
connectionOpened.set(true);
}
};
ProtonClientConnectionManager lifeCycleListener = new ProtonClientConnectionManager(new AMQPClientConnectionFactory(server, "myid", Collections.singletonMap(Symbol.getSymbol("myprop"), "propvalue"), 5000), Optional.of(eventHandler), clientSASLFactory);
ProtonClientProtocolManager protocolManager = new ProtonClientProtocolManager(new ProtonProtocolManagerFactory(), server);
NettyConnector connector = new NettyConnector(config, lifeCycleListener, lifeCycleListener, server.getExecutorFactory().getExecutor(), server.getExecutorFactory().getExecutor(), server.getScheduledPool(), protocolManager);
connector.start();
Object connectionId = connector.createConnection().getID();
assertNotNull(connectionId);
RemotingConnection remotingConnection = lifeCycleListener.getConnection(connectionId);
AtomicReference<ActiveMQException> ex = new AtomicReference<>();
AtomicBoolean closed = new AtomicBoolean(false);
remotingConnection.addCloseListener(() -> closed.set(true));
remotingConnection.addFailureListener(new FailureListener() {
@Override
public void connectionFailed(ActiveMQException exception, boolean failedOver) {
ex.set(exception);
}
@Override
public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
ex.set(exception);
}
});
try {
Wait.assertEquals(1, remote::getConnectionCount);
Wait.assertTrue(connectionOpened::get);
if (closeFromClient) {
lifeCycleListener.stop();
} else {
remote.stop();
}
Wait.assertEquals(0, remote::getConnectionCount);
assertTrue(remotingConnection.isDestroyed());
if (!closeFromClient) {
assertTrue(ex.get() instanceof ActiveMQRemoteDisconnectException);
} else {
assertNull(ex.get());
}
} finally {
if (closeFromClient) {
remote.stop();
} else {
lifeCycleListener.stop();
}
}
}
use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.
the class ActiveMQServerControlImpl method closeConnectionsForAddress.
@Override
public boolean closeConnectionsForAddress(final String ipAddress) {
checkStarted();
clearIO();
try {
boolean closed = false;
Set<RemotingConnection> connections = remotingService.getConnections();
for (RemotingConnection connection : connections) {
String remoteAddress = connection.getRemoteAddress();
if (remoteAddress.contains(ipAddress)) {
connection.fail(ActiveMQMessageBundle.BUNDLE.connectionsClosedByManagement(ipAddress));
remotingService.removeConnection(connection.getID());
closed = true;
}
}
return closed;
} finally {
blockOnIO();
}
}
use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.
the class ActiveMQServerControlImpl method closeConsumerConnectionsForAddress.
@Override
public boolean closeConsumerConnectionsForAddress(final String address) {
boolean closed = false;
checkStarted();
clearIO();
try {
for (Binding binding : postOffice.getMatchingBindings(SimpleString.toSimpleString(address)).getBindings()) {
if (binding instanceof LocalQueueBinding) {
Queue queue = ((LocalQueueBinding) binding).getQueue();
for (Consumer consumer : queue.getConsumers()) {
if (consumer instanceof ServerConsumer) {
ServerConsumer serverConsumer = (ServerConsumer) consumer;
RemotingConnection connection = null;
for (RemotingConnection potentialConnection : remotingService.getConnections()) {
if (potentialConnection.getID().toString().equals(serverConsumer.getConnectionID())) {
connection = potentialConnection;
}
}
if (connection != null) {
remotingService.removeConnection(connection.getID());
connection.fail(ActiveMQMessageBundle.BUNDLE.consumerConnectionsClosedByManagement(address));
closed = true;
}
}
}
}
}
} catch (Exception e) {
ActiveMQServerLogger.LOGGER.failedToCloseConsumerConnectionsForAddress(address, e);
} finally {
blockOnIO();
}
return closed;
}
Aggregations