/** * Construct an instance using an existing spring-managed connection factory. * @param connectionFactoryBean the spring-managed bean. */ public TcpOutboundGatewaySpec(AbstractClientConnectionFactory connectionFactoryBean) { this.target = new TcpOutboundGateway(); this.connectionFactory = null; this.target.setConnectionFactory(connectionFactoryBean); }
/** * Specify the Spring Integration reply channel. If this property is not * set the gateway will check for a 'replyChannel' header on the request. * @param replyChannel The reply channel. */ public void setReplyChannel(MessageChannel replyChannel) { this.setOutputChannel(replyChannel); }
@Bean @ServiceActivator(inputChannel = "toTcp") public MessageHandler tcpOutGate(AbstractClientConnectionFactory connectionFactory) { TcpOutboundGateway gate = new TcpOutboundGateway(); gate.setConnectionFactory(connectionFactory); gate.setOutputChannelName("resultToString"); return gate; }
final TcpOutboundGateway gateway = new TcpOutboundGateway(); gateway.setConnectionFactory(ccf); gateway.setRequestTimeout(Integer.MAX_VALUE); QueueChannel replyChannel = new QueueChannel(); gateway.setRequiresReply(true); gateway.setOutputChannel(replyChannel); Mockito.eq(Long.class))).thenReturn(50L, 10000L); gateway.setRemoteTimeoutExpression(remoteTimeoutExpression); final int j = i; results[j] = (this.executor.submit(() -> { gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build()); return j; })); done.set(true); assertEquals(0, TestUtils.getPropertyValue(gateway, "pendingReplies", Map.class).size()); gateway.stop(); ccf.stop();
final TcpOutboundGateway gateway = new TcpOutboundGateway(); gateway.setConnectionFactory(ccf); gateway.setRequestTimeout(Integer.MAX_VALUE); QueueChannel replyChannel = new QueueChannel(); gateway.setRequiresReply(true); gateway.setOutputChannel(replyChannel); gateway.setRemoteTimeoutExpression(new SpelExpressionParser().parseExpression("5000")); gateway.setBeanFactory(mock(BeanFactory.class)); gateway.afterPropertiesSet(); gateway.start(); try { gateway.handleMessage(MessageBuilder.withPayload("Test").build()); fail("expected failure"); done.set(true); ccf.getConnection(); gateway.stop(); ccf.stop();
TcpOutboundGateway gateway = new TcpOutboundGateway(); gateway.setConnectionFactory(cachingFactory); PollableChannel outputChannel = new QueueChannel(); gateway.setOutputChannel(outputChannel); gateway.setBeanFactory(mock(BeanFactory.class)); gateway.afterPropertiesSet(); gateway.start(); gateway.handleMessage(message); Message<?> reply = outputChannel.receive(0); assertNotNull(reply); assertEquals("bar", reply.getPayload()); done.set(true); gateway.stop(); verify(mockConn1).send(Mockito.any(Message.class)); factory2.stop();
@Bean @ServiceActivator(inputChannel = "out") public TcpOutboundGateway outGate() { TcpOutboundGateway outGate = new TcpOutboundGateway(); outGate.setConnectionFactory(tacf()); outGate.setReplyChannelName("toString"); return outGate; }
ccf.setSingleUse(true); ccf.start(); TcpOutboundGateway gateway = new TcpOutboundGateway(); gateway.setConnectionFactory(ccf); QueueChannel replyChannel = new QueueChannel(); gateway.setRequiresReply(true); gateway.setOutputChannel(replyChannel); gateway.setSendTimeout(123); gateway.setRemoteTimeout(60000); gateway.setSendTimeout(61000); gateway.setRequestTimeout(60000); for (int i = 100; i < 200; i++) { gateway.handleMessage(MessageBuilder.withPayload("Test" + i).build());
clientNio.setSoTimeout(1000); clientNio.afterPropertiesSet(); final TcpOutboundGateway out = new TcpOutboundGateway(); out.setConnectionFactory(clientNio); QueueChannel outputChannel = new QueueChannel(); out.setOutputChannel(outputChannel); out.setRemoteTimeout(60000); out.setBeanFactory(mock(BeanFactory.class)); out.afterPropertiesSet(); out.start(); Runnable command = () -> { try { out.handleMessage(MessageBuilder.withPayload("\u0004Test").build());
@Test public void testOutboundLength() throws Exception { TcpOutboundGateway gateway = new TcpOutboundGateway(); lengthHeaderClient.start(); gateway.setConnectionFactory(lengthHeaderClient); Message<String> message = MessageBuilder.withPayload("test").build(); @SuppressWarnings("unchecked") byte[] bytes = ((Message<byte[]>) gateway.handleRequestMessage(message)).getPayload(); assertEquals("echo:test", new String(bytes)); }
failFactory.setBeanFactory(mock(BeanFactory.class)); failFactory.afterPropertiesSet(); TcpOutboundGateway outGateway = new TcpOutboundGateway(); outGateway.setConnectionFactory(failFactory); outGateway.start(); QueueChannel replyChannel = new QueueChannel(); outGateway.setReplyChannel(replyChannel); Message<String> message = new GenericMessage<String>("foo"); outGateway.setRemoteTimeout(120000); outGateway.handleMessage(message); Socket socket = null; if (!singleUse) { TestingUtilities.waitStopListening(holder.server1, 10000L); TestingUtilities.waitUntilFactoryHasThisNumberOfConnections(client1, 0); outGateway.handleMessage(message); if (!singleUse) { socket = getSocket(client2); assertNotNull(replyMessage); holder.gateway2.stop(); outGateway.stop();
failoverClient.afterPropertiesSet(); TcpOutboundGateway outbound = new TcpOutboundGateway(); outbound.setConnectionFactory(failoverClient); QueueChannel replyChannel = new QueueChannel(); replyChannel.setBeanName("replyChannel"); outbound.setReplyChannel(replyChannel); outbound.setBeanFactory(mock(BeanFactory.class)); outbound.afterPropertiesSet(); outbound.start(); outbound.handleMessage(new GenericMessage<String>("foo")); Message<byte[]> result = (Message<byte[]>) replyChannel.receive(10000); assertNotNull(result); outbound.handleMessage(new GenericMessage<String>("foo")); result = (Message<byte[]>) replyChannel.receive(10000); assertNotNull(result); outbound.stop();
@Bean @ServiceActivator(inputChannel = "out1") public TcpOutboundGateway outGate1() { TcpOutboundGateway outGate = new TcpOutboundGateway(); outGate.setConnectionFactory(cf1()); outGate.setRemoteTimeout(50000); outGate.setReplyChannelName("toString"); return outGate; }
@Test public void testOutboundGatewayNoConnectionEvents() { TcpOutboundGateway gw = new TcpOutboundGateway(); AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) { gw.setConnectionFactory(ccf); DirectChannel requestChannel = new DirectChannel(); requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message)); gw.start(); Message<String> message = MessageBuilder.withPayload("foo") .setHeader(IpHeaders.CONNECTION_ID, "bar") .build(); gw.onMessage(message); assertNotNull(theEvent.get()); TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get(); gw.onMessage(message); assertNotNull(theEvent.get()); event = (TcpConnectionFailedCorrelationEvent) theEvent.get(); assertSame(message, messagingException.getFailedMessage()); assertEquals("Cannot correlate response - no connection id", messagingException.getMessage()); gw.stop(); ccf.stop();
/** * Specify the Spring Integration reply channel name. If this property is not * set the gateway will check for a 'replyChannel' header on the request. * @param replyChannel The reply channel. * @since 5.0 */ public void setReplyChannelName(String replyChannel) { this.setOutputChannelName(replyChannel); }
/** * @param remoteTimeout the remote timeout to set. * @return the spec. * @see TcpOutboundGateway#setRemoteTimeout(long) */ public TcpOutboundGatewaySpec remoteTimeout(long remoteTimeout) { this.target.setRemoteTimeout(remoteTimeout); return _this(); }
/** * Configure a {@link Function} that will be invoked at runtime to determine the destination to * which a message will be sent. Typically used with a Java 8 Lambda expression: * <pre class="code"> * {@code * .remoteTimeout(m -> m.getHeaders().get('rto')) * } * </pre> * @param remoteTimeoutFunction the function. * @param <P> the message payload type. * @return the spec. * @see TcpOutboundGateway#setRemoteTimeoutExpression(org.springframework.expression.Expression) */ public <P> TcpOutboundGatewaySpec remoteTimeout(Function<Message<P>, ?> remoteTimeoutFunction) { this.target.setRemoteTimeoutExpression(new FunctionExpression<>(remoteTimeoutFunction)); return _this(); }
@Override protected void doInit() { super.doInit(); if (!this.evaluationContextSet) { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); } }
@Override public boolean onMessage(Message<?> message) { String connectionId = message.getHeaders().get(IpHeaders.CONNECTION_ID, String.class); if (connectionId == null) { logger.error("Cannot correlate response - no connection id"); publishNoConnectionEvent(message, null, "Cannot correlate response - no connection id"); return false; } if (logger.isTraceEnabled()) { logger.trace("onMessage: " + connectionId + "(" + message + ")"); } AsyncReply reply = this.pendingReplies.get(connectionId); if (reply == null) { if (message instanceof ErrorMessage) { /* * Socket errors are sent here so they can be conveyed to any waiting thread. * If there's not one, simply ignore. */ return false; } else { String errorMessage = "Cannot correlate response - no pending reply for " + connectionId; logger.error(errorMessage); publishNoConnectionEvent(message, connectionId, errorMessage); return false; } } reply.setReply(message); return false; }
ccf.setSingleUse(false); ccf.start(); TcpOutboundGateway gateway = new TcpOutboundGateway(); gateway.setConnectionFactory(ccf); QueueChannel replyChannel = new QueueChannel(); gateway.setRequiresReply(true); gateway.setOutputChannel(replyChannel); for (int i = 100; i < 110; i++) { gateway.handleMessage(MessageBuilder.withPayload("Test" + i).build()); gateway.stop(); ccf.stop(); serverSocket.get().close();