Search in sources :

Example 1 with StompConnectionFailedEvent

use of org.springframework.integration.stomp.event.StompConnectionFailedEvent in project spring-integration by spring-projects.

the class StompServerIntegrationTests method testStompAdapters.

@Test
public void testStompAdapters() throws Exception {
    ConfigurableApplicationContext context1 = new AnnotationConfigApplicationContext(ContextConfiguration.class);
    ConfigurableApplicationContext context2 = new AnnotationConfigApplicationContext(ContextConfiguration.class);
    PollableChannel stompEvents1 = context1.getBean("stompEvents", PollableChannel.class);
    PollableChannel stompEvents2 = context2.getBean("stompEvents", PollableChannel.class);
    PollableChannel stompInputChannel1 = context1.getBean("stompInputChannel", PollableChannel.class);
    PollableChannel stompInputChannel2 = context2.getBean("stompInputChannel", PollableChannel.class);
    MessageChannel stompOutputChannel1 = context1.getBean("stompOutputChannel", MessageChannel.class);
    MessageChannel stompOutputChannel2 = context2.getBean("stompOutputChannel", MessageChannel.class);
    Message<?> eventMessage;
    do {
        eventMessage = stompEvents1.receive(10000);
    } while (eventMessage != null && !(eventMessage.getPayload() instanceof StompSessionConnectedEvent));
    assertNotNull(eventMessage);
    eventMessage = stompEvents1.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    StompReceiptEvent stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompSessionConnectedEvent.class));
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    stompOutputChannel1.send(new GenericMessage<byte[]>("Hello, Client#2!".getBytes()));
    Message<?> receive11 = stompInputChannel1.receive(10000);
    Message<?> receive21 = stompInputChannel2.receive(10000);
    assertNotNull(receive11);
    assertNotNull(receive21);
    assertArrayEquals("Hello, Client#2!".getBytes(), (byte[]) receive11.getPayload());
    assertArrayEquals("Hello, Client#2!".getBytes(), (byte[]) receive21.getPayload());
    stompOutputChannel2.send(new GenericMessage<byte[]>("Hello, Client#1!".getBytes()));
    Message<?> receive12 = stompInputChannel1.receive(10000);
    Message<?> receive22 = stompInputChannel2.receive(10000);
    assertNotNull(receive12);
    assertNotNull(receive22);
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) receive12.getPayload());
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) receive22.getPayload());
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SEND, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) stompReceiptEvent.getMessage().getPayload());
    Lifecycle stompInboundChannelAdapter2 = context2.getBean("stompInboundChannelAdapter", Lifecycle.class);
    stompInboundChannelAdapter2.stop();
    stompOutputChannel1.send(new GenericMessage<byte[]>("How do you do?".getBytes()));
    Message<?> receive13 = stompInputChannel1.receive(10000);
    assertNotNull(receive13);
    Message<?> receive23 = stompInputChannel2.receive(100);
    assertNull(receive23);
    stompInboundChannelAdapter2.start();
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    stompOutputChannel1.send(new GenericMessage<byte[]>("???".getBytes()));
    Message<?> receive24 = stompInputChannel2.receive(10000);
    assertNotNull(receive24);
    assertArrayEquals("???".getBytes(), (byte[]) receive24.getPayload());
    activeMQBroker.stop();
    do {
        eventMessage = stompEvents1.receive(10000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompConnectionFailedEvent));
    try {
        stompOutputChannel1.send(new GenericMessage<byte[]>("foo".getBytes()));
        fail("MessageDeliveryException is expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), containsString("could not deliver message"));
    }
    activeMQBroker.start(false);
    do {
        eventMessage = stompEvents1.receive(20000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompReceiptEvent));
    do {
        eventMessage = stompEvents2.receive(10000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompReceiptEvent));
    stompOutputChannel1.send(new GenericMessage<byte[]>("foo".getBytes()));
    Message<?> receive25 = stompInputChannel2.receive(10000);
    assertNotNull(receive25);
    assertArrayEquals("foo".getBytes(), (byte[]) receive25.getPayload());
    context1.close();
    context2.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) StompReceiptEvent(org.springframework.integration.stomp.event.StompReceiptEvent) StompConnectionFailedEvent(org.springframework.integration.stomp.event.StompConnectionFailedEvent) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) Lifecycle(org.springframework.context.Lifecycle) PollableChannel(org.springframework.messaging.PollableChannel) StompSessionConnectedEvent(org.springframework.integration.stomp.event.StompSessionConnectedEvent) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 2 with StompConnectionFailedEvent

use of org.springframework.integration.stomp.event.StompConnectionFailedEvent in project spring-integration by spring-projects.

the class AbstractStompSessionManager method scheduleReconnect.

private void scheduleReconnect(Throwable e) {
    this.epoch.incrementAndGet();
    this.connecting = this.connected = false;
    if (e != null) {
        this.logger.error("STOMP connect error for " + this, e);
    }
    if (this.applicationEventPublisher != null) {
        this.applicationEventPublisher.publishEvent(new StompConnectionFailedEvent(this, e));
    }
    // cancel() after the publish in case we are on that thread; a send to a QueueChannel would fail.
    if (this.reconnectFuture != null) {
        this.reconnectFuture.cancel(true);
        this.reconnectFuture = null;
    }
    if (this.stompClient.getTaskScheduler() != null) {
        this.reconnectFuture = this.stompClient.getTaskScheduler().schedule(this::connect, new Date(System.currentTimeMillis() + this.recoveryInterval));
    } else {
        this.logger.info("For automatic reconnection the 'stompClient' should be configured with a TaskScheduler.");
    }
}
Also used : StompConnectionFailedEvent(org.springframework.integration.stomp.event.StompConnectionFailedEvent) Date(java.util.Date)

Aggregations

StompConnectionFailedEvent (org.springframework.integration.stomp.event.StompConnectionFailedEvent)2 Date (java.util.Date)1 Test (org.junit.Test)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 Lifecycle (org.springframework.context.Lifecycle)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 StompReceiptEvent (org.springframework.integration.stomp.event.StompReceiptEvent)1 StompSessionConnectedEvent (org.springframework.integration.stomp.event.StompSessionConnectedEvent)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1 PollableChannel (org.springframework.messaging.PollableChannel)1