/** * Construct an instance using an existing spring-managed connection factory. * @param connectionFactoryBean the spring-managed bean. */ TcpOutboundChannelAdapterSpec(AbstractConnectionFactory connectionFactoryBean) { this.target = new TcpSendingMessageHandler(); this.connectionFactory = null; this.target.setConnectionFactory(connectionFactoryBean); }
/** * @param isClientMode true to connect in client mode * @return the spec. * @see TcpSendingMessageHandler#setClientMode(boolean) */ public TcpOutboundChannelAdapterSpec clientMode(boolean isClientMode) { this.target.setClientMode(isClientMode); return _this(); }
/** * @param retryInterval the client mode retry interval to set. * @return the spec. * @see TcpSendingMessageHandler#setRetryInterval(long) */ public TcpOutboundChannelAdapterSpec retryInterval(long retryInterval) { this.target.setRetryInterval(retryInterval); return _this(); }
/** * Method that actually does the write. * @param message The message to write. * @return the connection. */ protected TcpConnection doWrite(Message<?> message) { TcpConnection connection = null; try { connection = obtainConnection(message); if (logger.isDebugEnabled()) { logger.debug("Got Connection " + connection.getConnectionId()); } connection.send(message); } catch (Exception ex) { String connectionId = null; if (connection != null) { connectionId = connection.getConnectionId(); } throw wrapToMessageHandlingExceptionIfNecessary(message, "Failed to handle message using " + connectionId, ex); } return connection; }
private void handleMessageAsServer(Message<?> message) { // We don't own the connection, we are asynchronously replying String connectionId = message.getHeaders().get(IpHeaders.CONNECTION_ID, String.class); TcpConnection connection = null; if (connectionId != null) { connection = this.connections.get(connectionId); } if (connection != null) { try { connection.send(message); } catch (Exception ex) { logger.error("Error sending message", ex); connection.close(); throw wrapToMessageHandlingExceptionIfNecessary(message, "Error sending message", ex); } finally { if (this.isSingleUse) { // close after replying connection.close(); } } } else { logger.error("Unable to find outbound socket for " + message); MessageHandlingException messageHandlingException = new MessageHandlingException(message, "Unable to find outbound socket"); publishNoConnectionEvent(messageHandlingException, connectionId); throw messageHandlingException; } }
@Test public void testConnectionException() throws Exception { TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); AbstractConnectionFactory mockCcf = mock(AbstractClientConnectionFactory.class); Mockito.doAnswer(invocation -> { throw new SocketException("Failed to connect"); }).when(mockCcf).getConnection(); handler.setConnectionFactory(mockCcf); try { handler.handleMessage(new GenericMessage<String>("foo")); fail("Expected exception"); } catch (Exception e) { assertTrue(e instanceof MessagingException); assertTrue(e.getCause() != null); assertTrue(e.getCause() instanceof SocketException); assertEquals("Failed to connect", e.getCause().getMessage()); } }
ccf.setDeserializer(serializer); ccf.setSoTimeout(Integer.MAX_VALUE); TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); handler.setConnectionFactory(ccf); TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter(); adapter.setConnectionFactory(ccf); QueueChannel channel = new QueueChannel(); adapter.setOutputChannel(channel); handler.setClientMode(true); handler.setRetryInterval(10000); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(1); taskScheduler.initialize(); handler.setTaskScheduler(taskScheduler); handler.start(); adapter.start(); handler.handleMessage(MessageBuilder.withPayload("Test").build()); handler.handleMessage(MessageBuilder.withPayload("Test").build()); Message<?> mOut = channel.receive(10000); assertNotNull(mOut); assertEquals("Reply2", new String((byte[]) mOut.getPayload())); done.set(true); handler.stop(); handler.start(); handler.stop(); adapter.stop();
TcpNetServerConnectionFactory in = new TcpNetServerConnectionFactory(0); in.setApplicationEventPublisher(mock(ApplicationEventPublisher.class)); final TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); handler.setConnectionFactory(in); final AtomicInteger count = new AtomicInteger(2); in.registerListener(message -> { handler.handleMessage(message); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.start(); TestingUtilities.waitListening(in, null); int port = in.getPort(); assertNotNull(result); assertEquals("bar", new String(result.getPayload())); handler.stop(); gate.stop(); verify(logger, never()).error(anyString());
@Test public void testOutboundChannelAdapterNoConnectionEvents() { TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); AbstractServerConnectionFactory scf = new AbstractServerConnectionFactory(0) { handler.setConnectionFactory(scf); handler.start(); Message<String> message = MessageBuilder.withPayload("foo") .setHeader(IpHeaders.CONNECTION_ID, "bar") .build(); try { handler.handleMessage(message); fail("expected exception");
/** * @param taskScheduler the scheduler for connecting in client mode. * @return the spec. * @see TcpSendingMessageHandler#setTaskScheduler(TaskScheduler) */ public TcpOutboundChannelAdapterSpec taskScheduler(TaskScheduler taskScheduler) { this.target.setTaskScheduler(taskScheduler); return _this(); }
MessageHandlingException messageHandlingException = new MessageHandlingException(message, "Unable to find outbound socket"); publishNoConnectionEvent(messageHandlingException, (String) connectionId); throw messageHandlingException; connection = doWrite(message); logger.debug("Fail on first write attempt", e); connection = doWrite(message);
@Override public void start() { synchronized (this.lifecycleMonitor) { if (!this.active) { this.active = true; if (this.clientConnectionFactory != null) { this.clientConnectionFactory.start(); } if (this.serverConnectionFactory != null) { this.serverConnectionFactory.start(); } if (this.isClientMode) { Assert.notNull(this.clientConnectionFactory, "For client-mode, connection factory must be type='client'"); ClientModeConnectionManager manager = new ClientModeConnectionManager(this.clientConnectionFactory); this.clientModeConnectionManager = manager; Assert.state(getTaskScheduler() != null, "Client mode requires a task scheduler"); this.scheduledFuture = getTaskScheduler().scheduleAtFixedRate(manager, this.retryInterval); } } } }
private void handleMessageAsClient(Message<?> message) { // we own the connection TcpConnection connection = null; try { connection = doWrite(message); } catch (MessageHandlingException e) { // retry - socket may have closed if (e.getCause() instanceof IOException) { if (logger.isDebugEnabled()) { logger.debug("Fail on first write attempt", e); } connection = doWrite(message); } else { throw e; } } finally { if (connection != null && this.isSingleUse && this.clientConnectionFactory.getListener() == null) { // if there's no collaborating inbound adapter, close immediately, otherwise // it will close after receiving the reply. connection.close(); } } }
/** * Method that actually does the write. * @param message The message to write. * @return the connection. */ protected TcpConnection doWrite(Message<?> message) { TcpConnection connection = null; try { connection = obtainConnection(message); if (logger.isDebugEnabled()) { logger.debug("Got Connection " + connection.getConnectionId()); } connection.send(message); } catch (Exception e) { String connectionId = null; if (connection != null) { connectionId = connection.getConnectionId(); } if (e instanceof MessageHandlingException) { throw (MessageHandlingException) e; } throw new MessageHandlingException(message, "Failed to handle message using " + connectionId, e); } return connection; }
ccf.setSingleUse(true); ccf.start(); TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); handler.setConnectionFactory(ccf); handler.handleMessage(MessageBuilder.withPayload("Test").build()); done.set(true); ccf.stop();
/** * @param taskScheduler the scheduler for connecting in client mode. * @return the spec. * @see TcpSendingMessageHandler#setTaskScheduler(TaskScheduler) */ public TcpOutboundChannelAdapterSpec taskScheduler(TaskScheduler taskScheduler) { this.target.setTaskScheduler(taskScheduler); return _this(); }
@Override public void start() { synchronized (this.lifecycleMonitor) { if (!this.active) { this.active = true; if (this.clientConnectionFactory != null) { this.clientConnectionFactory.start(); } if (this.serverConnectionFactory != null) { this.serverConnectionFactory.start(); } if (this.isClientMode) { ClientModeConnectionManager manager = new ClientModeConnectionManager( this.clientConnectionFactory); this.clientModeConnectionManager = manager; Assert.state(this.getTaskScheduler() != null, "Client mode requires a task scheduler"); this.scheduledFuture = this.getTaskScheduler().scheduleAtFixedRate(manager, this.retryInterval); } } } }
ccf.start(); ccf.setSingleUse(true); TcpSendingMessageHandler handler = new TcpSendingMessageHandler(); handler.setConnectionFactory(ccf); handler.handleMessage(MessageBuilder.withPayload("Test.1").build()); handler.handleMessage(MessageBuilder.withPayload("Test.2").build()); assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS)); done.set(true);
/** * Construct an instance using the provided connection factory spec. * @param connectionFactorySpec the spec. */ TcpOutboundChannelAdapterSpec(AbstractConnectionFactorySpec<?, ?> connectionFactorySpec) { this.target = new TcpSendingMessageHandler(); this.connectionFactory = connectionFactorySpec.get(); this.target.setConnectionFactory(this.connectionFactory); }
/** * @param isClientMode true to connect in client mode * @return the spec. * @see TcpSendingMessageHandler#setClientMode(boolean) */ public TcpOutboundChannelAdapterSpec clientMode(boolean isClientMode) { this.target.setClientMode(isClientMode); return _this(); }