Example usage for org.springframework.integration.ip IpHeaders CONNECTION_ID

List of usage examples for org.springframework.integration.ip IpHeaders CONNECTION_ID

Introduction

In this page you can find the example usage for org.springframework.integration.ip IpHeaders CONNECTION_ID.

Prototype

String CONNECTION_ID

To view the source code for org.springframework.integration.ip IpHeaders CONNECTION_ID.

Click Source Link

Document

A unique identifier for a TCP connection; set by the framework for inbound messages; when sending to a server-side inbound channel adapter, or replying to an inbound gateway, this header is required so the endpoint can determine which connection to send the message to.

Usage

From source file:ru.jts_dev.authserver.service.BroadcastService.java

private void send(String connectionId, OutgoingMessageWrapper message) {
    message.getHeaders().put(IpHeaders.CONNECTION_ID, connectionId);
    packetChannel.send(message);
}

From source file:org.bitcoinrt.server.ConnectionBroker.java

public List<Message<?>> createBroadCastMessages(Message<?> message) {

    final List<Message<?>> messages = new ArrayList<Message<?>>();

    for (String connectionId : clients.keySet()) {
        messages.add(/*from w w w.ja v  a2  s  . c  o m*/
                MessageBuilder.fromMessage(message).setHeader(IpHeaders.CONNECTION_ID, connectionId).build());
    }

    return messages;

}

From source file:ru.jts_dev.gameserver.service.BroadcastService.java

public final void send(final String connectionId, OutgoingMessageWrapper message) {
    if (message.isStatic() && message instanceof StaticOutgoingMessageWrapper) {
        send(connectionId, message);// w  w w .  ja  v  a 2s.c  om
        logger.trace("Clone {} packet", message.getClass().getSimpleName());
        return;
    }
    message.getHeaders().put(IpHeaders.CONNECTION_ID, connectionId);
    packetChannel.send(message);
}

From source file:ru.jts_dev.common.packets.IncomingMessageWrapper.java

public final String getConnectionId() {
    if (!headers.containsKey(IpHeaders.CONNECTION_ID))
        throw new RuntimeException("connectionId header not present in headers");
    return (String) headers.get(IpHeaders.CONNECTION_ID);
}

From source file:ru.jts_dev.authserver.config.AuthIntegrationConfig.java

/**
 * Event listener for {@link TcpConnectionEvent}.
 * Event receives when new connection accepted.
 *
 * @param event - event instance//from w  w w.  j a  v a 2 s.c  o  m
 */
@EventListener
public void authTcpConnectionEventListener(TcpConnectionEvent event) {
    String connectionId = event.getConnectionId();

    AuthSession gameSession = authSessionService.getSessionBy(connectionId);
    byte[] scrambledModulus = scrambleModulus(
            ((RSAPublicKey) gameSession.getRsaKeyPair().getPublic()).getModulus());
    byte[] blowfishKey = gameSession.getBlowfishKey();

    OutgoingMessageWrapper msg = new Init(gameSession.getSessionId(), scrambledModulus, blowfishKey);
    msg.getHeaders().put(IpHeaders.CONNECTION_ID, event.getConnectionId());

    packetChannel().send(msg);
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void integrationTest() throws Exception {
    TestingUtilities.waitListening(serverCf, null);
    new DirectFieldAccessor(this.clientAdapterCf).setPropertyValue("port", this.serverCf.getPort());

    this.outbound.send(new GenericMessage<>("Hello, world!"));
    Message<?> m = inbound.receive(10000);
    assertNotNull(m);/*from www.j  av  a2s  .c  o  m*/
    String connectionId = m.getHeaders().get(IpHeaders.CONNECTION_ID, String.class);

    // assert we use the same connection from the pool
    outbound.send(new GenericMessage<String>("Hello, world!"));
    m = inbound.receive(10000);
    assertNotNull(m);
    assertEquals(connectionId, m.getHeaders().get(IpHeaders.CONNECTION_ID, String.class));
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
//   @Repeat(1000) // INT-3722
public void gatewayIntegrationTest() throws Exception {
    final List<String> connectionIds = new ArrayList<String>();
    final AtomicBoolean okToRun = new AtomicBoolean(true);
    Executors.newSingleThreadExecutor().execute(() -> {
        while (okToRun.get()) {
            Message<?> m = inbound.receive(1000);
            if (m != null) {
                connectionIds.add((String) m.getHeaders().get(IpHeaders.CONNECTION_ID));
                replies.send(MessageBuilder.withPayload("foo:" + new String((byte[]) m.getPayload()))
                        .copyHeaders(m.getHeaders()).build());
            }/*from  w w w  .j a va 2 s  .  co m*/
        }
    });
    TestingUtilities.waitListening(serverCf, null);
    new DirectFieldAccessor(this.clientGatewayCf).setPropertyValue("port", this.serverCf.getPort());

    this.toGateway.send(new GenericMessage<>("Hello, world!"));
    Message<?> m = fromGateway.receive(1000);
    assertNotNull(m);
    assertEquals("foo:" + "Hello, world!", new String((byte[]) m.getPayload()));

    BlockingQueue<?> connections = TestUtils.getPropertyValue(this.gatewayCF, "pool.available",
            BlockingQueue.class);
    // wait until the connection is returned to the pool
    int n = 0;
    while (n++ < 100 && connections.size() == 0) {
        Thread.sleep(100);
    }

    // assert we use the same connection from the pool
    toGateway.send(new GenericMessage<String>("Hello, world2!"));
    m = fromGateway.receive(1000);
    assertNotNull(m);
    assertEquals("foo:" + "Hello, world2!", new String((byte[]) m.getPayload()));

    assertEquals(2, connectionIds.size());
    assertEquals(connectionIds.get(0), connectionIds.get(1));

    okToRun.set(false);
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test //INT-3650
public void testRealConnection() throws Exception {
    TcpNetServerConnectionFactory in = new TcpNetServerConnectionFactory(0);
    final CountDownLatch latch1 = new CountDownLatch(2);
    final CountDownLatch latch2 = new CountDownLatch(102);
    final List<String> connectionIds = new ArrayList<String>();
    in.registerListener(message -> {/* w w  w .  j av a 2 s .  c  om*/
        connectionIds.add((String) message.getHeaders().get(IpHeaders.CONNECTION_ID));
        latch1.countDown();
        latch2.countDown();
        return false;
    });
    in.start();
    TestingUtilities.waitListening(in, null);
    int port = in.getPort();
    TcpNetClientConnectionFactory out = new TcpNetClientConnectionFactory("localhost", port);
    CachingClientConnectionFactory cache = new CachingClientConnectionFactory(out, 1);
    cache.setSingleUse(false);
    cache.setConnectionWaitTimeout(100);
    cache.start();
    TcpConnectionSupport connection1 = cache.getConnection();
    connection1.send(new GenericMessage<String>("foo"));
    connection1.close();
    TcpConnectionSupport connection2 = cache.getConnection();
    connection2.send(new GenericMessage<String>("foo"));
    connection2.close();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(1));
    for (int i = 0; i < 100; i++) {
        TcpConnectionSupport connection = cache.getConnection();
        connection.send(new GenericMessage<String>("foo"));
        connection.close();
    }
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(101));
    in.stop();
    cache.stop();
}

From source file:org.springframework.integration.ip.tcp.connection.ConnectionEventTests.java

@Test
public void testOutboundChannelAdapterNoConnectionEvents() {
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    AbstractServerConnectionFactory scf = new AbstractServerConnectionFactory(0) {

        @Override/*from  w  w w .  j  a va  2 s. com*/
        public void run() {
        }
    };
    final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
    scf.setApplicationEventPublisher(new ApplicationEventPublisher() {

        @Override
        public void publishEvent(Object event) {
        }

        @Override
        public void publishEvent(ApplicationEvent event) {
            theEvent.set(event);
        }

    });
    handler.setConnectionFactory(scf);
    handler.start();
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar")
            .build();
    try {
        handler.handleMessage(message);
        fail("expected exception");
    } catch (MessageHandlingException e) {
        assertThat(e.getMessage(), Matchers.containsString("Unable to find outbound socket"));
    }
    assertNotNull(theEvent.get());
    TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertEquals("bar", event.getConnectionId());
    assertSame(message, ((MessagingException) event.getCause()).getFailedMessage());
}

From source file:org.springframework.integration.ip.tcp.connection.ConnectionEventTests.java

@Test
public void testInboundGatewayNoConnectionEvents() {
    TcpInboundGateway gw = new TcpInboundGateway();
    AbstractServerConnectionFactory scf = new AbstractServerConnectionFactory(0) {

        @Override/*from   w w w.  j  a  v a2  s .c om*/
        public void run() {
        }
    };
    final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
    scf.setApplicationEventPublisher(new ApplicationEventPublisher() {

        @Override
        public void publishEvent(Object event) {
        }

        @Override
        public void publishEvent(ApplicationEvent event) {
            theEvent.set(event);
        }

    });
    gw.setConnectionFactory(scf);
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            ((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());
}