@Bean public TcpInboundGateway inGate(TcpNioServerConnectionFactory sf) { TcpInboundGateway inGate = new TcpInboundGateway(); inGate.setConnectionFactory(sf); inGate.setRequestChannelName("in"); return inGate; }
/** * @param isClientMode true to connect in client mode * @return the spec. * @see TcpInboundGateway#setClientMode(boolean) */ public TcpInboundGatewaySpec clientMode(boolean isClientMode) { this.target.setClientMode(isClientMode); return _this(); }
/** * @param retryInterval the client mode retry interval to set. * @return the spec. * @see TcpInboundGateway#setRetryInterval(long) */ public TcpInboundGatewaySpec retryInterval(long retryInterval) { this.target.setRetryInterval(retryInterval); return _this(); }
@Bean public TcpInboundGateway tcpInGate(AbstractServerConnectionFactory connectionFactory) { TcpInboundGateway inGate = new TcpInboundGateway(); inGate.setConnectionFactory(connectionFactory); inGate.setRequestChannel(fromTcp()); return inGate; }
/** * Construct an instance using an existing spring-managed connection factory. * @param connectionFactoryBean the spring-managed bean. */ TcpInboundGatewaySpec(AbstractConnectionFactory connectionFactoryBean) { super(new TcpInboundGateway()); this.connectionFactory = null; this.target.setConnectionFactory(connectionFactoryBean); }
AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(0); scf.setSingleUse(false); TcpInboundGateway gateway = new TcpInboundGateway(); gateway.setConnectionFactory(scf); scf.start(); TestingUtilities.waitListening(scf, 20000L); int port = scf.getPort(); final QueueChannel channel = new QueueChannel(); gateway.setRequestChannel(channel); gateway.setBeanFactory(mock(BeanFactory.class)); ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service()); handler.setBeanFactory(mock(BeanFactory.class)); assertTrue(results.remove("Echo:Test1\r\n")); assertTrue(results.remove("Echo:Test2\r\n")); gateway.stop(); scf.stop();
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port.get()); ccf.setSingleUse(false); TcpInboundGateway gateway = new TcpInboundGateway(); gateway.setConnectionFactory(ccf); final QueueChannel channel = new QueueChannel(); gateway.setRequestChannel(channel); gateway.setClientMode(true); gateway.setRetryInterval(10000); gateway.setBeanFactory(mock(BeanFactory.class)); gateway.afterPropertiesSet(); ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service()); handler.setBeanFactory(mock(BeanFactory.class)); taskScheduler.setPoolSize(1); taskScheduler.initialize(); gateway.setTaskScheduler(taskScheduler); gateway.start(); Message<?> message = channel.receive(10000); assertNotNull(message); assertTrue(latch3.await(10, TimeUnit.SECONDS)); assertTrue(done.get()); gateway.stop(); executorService.shutdown();
server1.setApplicationEventPublisher(NULL_PUBLISHER); server2.setApplicationEventPublisher(NULL_PUBLISHER); TcpInboundGateway gateway1 = new TcpInboundGateway(); gateway1.setConnectionFactory(server1); SubscribableChannel channel = new DirectChannel(); final AtomicReference<String> connectionId = new AtomicReference<String>(); ((MessageChannel) message.getHeaders().getReplyChannel()).send(message); }); gateway1.setRequestChannel(channel); gateway1.setBeanFactory(mock(BeanFactory.class)); gateway1.afterPropertiesSet(); gateway1.start(); TcpInboundGateway gateway2 = new TcpInboundGateway(); gateway2.setConnectionFactory(server2); gateway2.setRequestChannel(channel); gateway2.setBeanFactory(mock(BeanFactory.class)); gateway2.afterPropertiesSet(); gateway2.start(); TestingUtilities.waitListening(server1, null); TestingUtilities.waitListening(server2, null);
@Test public void testInboundGatewayNoConnectionEvents() { TcpInboundGateway gw = new TcpInboundGateway(); AbstractServerConnectionFactory scf = new AbstractServerConnectionFactory(0) { gw.setConnectionFactory(scf); DirectChannel requestChannel = new DirectChannel(); requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message)); gw.setRequestChannel(requestChannel); 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(); assertEquals("bar", event.getConnectionId()); assertSame(message, ((MessagingException) event.getCause()).getFailedMessage()); gw.stop(); scf.stop();
DirectChannel inChannel = new DirectChannel(); inChannel.setBeanName("inChannel"); TcpInboundGateway inbound = new TcpInboundGateway(); inbound.setConnectionFactory(server); inbound.setRequestChannel(inChannel); inbound.afterPropertiesSet(); inChannel.subscribe(new BridgeHandler()); inbound.start(); TestingUtilities.waitListening(server, 10000L); int port = server.getPort(); assertEquals("foo", new String(result.getPayload())); inbound.stop(); outbound.stop();
private boolean doOnMessage(Message<?> message) { Message<?> reply = this.sendAndReceiveMessage(message); if (reply == null) { if (logger.isDebugEnabled()) { logger.debug("null reply received for " + message + " nothing to send"); } return false; } String connectionId = (String) message.getHeaders().get(IpHeaders.CONNECTION_ID); TcpConnection connection = null; if (connectionId != null) { connection = this.connections.get(connectionId); } if (connection == null) { publishNoConnectionEvent(message, connectionId); logger.error("Connection not found when processing reply " + reply + " for " + message); return false; } try { connection.send(reply); } catch (Exception e) { logger.error("Failed to send reply", e); } return false; }
@Override public int afterShutdown() { this.stop(); return this.activeCount.get(); } }
/** * @param taskScheduler the scheduler for connecting in client mode. * @return the spec. * @see TcpInboundGateway#setTaskScheduler(TaskScheduler) */ public TcpInboundGatewaySpec taskScheduler(TaskScheduler taskScheduler) { this.target.setTaskScheduler(taskScheduler); return _this(); }
@Override // protected by super#lifecycleLock protected void doStart() { super.doStart(); if (!this.active) { this.active = true; this.shuttingDown = false; if (this.serverConnectionFactory != null) { this.serverConnectionFactory.start(); } if (this.clientConnectionFactory != null) { this.clientConnectionFactory.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); } } }
return doOnMessage(message);
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0); scf.setSingleUse(true); TcpInboundGateway gateway = new TcpInboundGateway(); gateway.setConnectionFactory(scf); SubscribableChannel errorChannel = new DirectChannel(); final String errorMessage = "An error occurred"; replyChannel.send(new GenericMessage<>(errorMessage)); }); gateway.setErrorChannel(errorChannel); scf.start(); TestingUtilities.waitListening(scf, 20000L); int port = scf.getPort(); final SubscribableChannel channel = new DirectChannel(); gateway.setRequestChannel(channel); gateway.setBeanFactory(mock(BeanFactory.class)); ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService()); channel.subscribe(handler); readFully(socket2.getInputStream(), bytes); assertEquals(errorMessage + "\r\n", new String(bytes)); gateway.stop(); scf.stop();
serverNio.setSerializer(serializer); serverNio.afterPropertiesSet(); TcpInboundGateway in = new TcpInboundGateway(); in.setConnectionFactory(serverNio); QueueChannel serverSideChannel = new QueueChannel(); in.setRequestChannel(serverSideChannel); in.setBeanFactory(mock(BeanFactory.class)); in.afterPropertiesSet(); in.start(); TestingUtilities.waitListening(serverNio, null); TcpNioClientConnectionFactory clientNio = new TcpNioClientConnectionFactory("localhost", serverNio.getPort());
/** * Construct an instance using a connection factory spec. * @param connectionFactorySpec the spec. */ TcpInboundGatewaySpec(AbstractConnectionFactorySpec<?, ?> connectionFactorySpec) { super(new TcpInboundGateway()); this.connectionFactory = connectionFactorySpec.get(); this.target.setConnectionFactory(this.connectionFactory); }
private boolean doOnMessage(Message<?> message) { Message<?> reply = this.sendAndReceiveMessage(message); if (reply == null) { if (logger.isDebugEnabled()) { logger.debug("null reply received for " + message + " nothing to send"); } return false; } String connectionId = (String) message.getHeaders().get(IpHeaders.CONNECTION_ID); TcpConnection connection = null; if (connectionId != null) { connection = this.connections.get(connectionId); } if (connection == null) { publishNoConnectionEvent(message, connectionId); logger.error("Connection not found when processing reply " + reply + " for " + message); return false; } try { connection.send(reply); } catch (Exception e) { logger.error("Failed to send reply", e); } return false; }
@Override public int afterShutdown() { this.stop(); return this.activeCount.get(); } }