Search in sources :

Example 1 with Lifecycle

use of org.springframework.context.Lifecycle in project dubbo by alibaba.

the class SpringStatusChecker method check.

public Status check() {
    ApplicationContext context = ServiceBean.getSpringContext();
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level = Status.Level.OK;
    if (context instanceof Lifecycle) {
        if (((Lifecycle) context).isRunning()) {
            level = Status.Level.OK;
        } else {
            level = Status.Level.ERROR;
        }
    } else {
        level = Status.Level.UNKNOWN;
    }
    StringBuilder buf = new StringBuilder();
    try {
        Class<?> cls = context.getClass();
        Method method = null;
        while (cls != null && method == null) {
            try {
                method = cls.getDeclaredMethod("getConfigLocations", new Class<?>[0]);
            } catch (NoSuchMethodException t) {
                cls = cls.getSuperclass();
            }
        }
        if (method != null) {
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            String[] configs = (String[]) method.invoke(context, new Object[0]);
            if (configs != null && configs.length > 0) {
                for (String config : configs) {
                    if (buf.length() > 0) {
                        buf.append(",");
                    }
                    buf.append(config);
                }
            }
        }
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    }
    return new Status(level, buf.toString());
}
Also used : Status(com.alibaba.dubbo.common.status.Status) Lifecycle(org.springframework.context.Lifecycle) Method(java.lang.reflect.Method) ApplicationContext(org.springframework.context.ApplicationContext)

Example 2 with Lifecycle

use of org.springframework.context.Lifecycle in project spring-integration by spring-projects.

the class IntegrationMBeanExporter method stopActiveChannels.

@ManagedOperation
public void stopActiveChannels() {
    // Stop any "active" channels (JMS etc).
    for (Entry<String, MessageChannelMetrics> entry : this.allChannelsByName.entrySet()) {
        MessageChannelMetrics metrics = entry.getValue();
        MessageChannel channel = (MessageChannel) metrics;
        if (channel instanceof Lifecycle) {
            if (logger.isInfoEnabled()) {
                logger.info("Stopping channel " + channel);
            }
            ((Lifecycle) channel).stop();
        }
    }
}
Also used : MessageChannelMetrics(org.springframework.integration.support.management.MessageChannelMetrics) MessageChannel(org.springframework.messaging.MessageChannel) Lifecycle(org.springframework.context.Lifecycle) ManagedOperation(org.springframework.jmx.export.annotation.ManagedOperation)

Example 3 with Lifecycle

use of org.springframework.context.Lifecycle 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 4 with Lifecycle

use of org.springframework.context.Lifecycle in project spring-integration by spring-projects.

the class ScatterGatherParserTests method testDistribution.

@Test
@SuppressWarnings("unchecked")
public void testDistribution() {
    MessageHandler scatterGather = this.beanFactory.getBean("scatterGather2.handler", MessageHandler.class);
    assertSame(this.beanFactory.getBean("gatherChannel"), TestUtils.getPropertyValue(scatterGather, "gatherChannel"));
    Lifecycle gatherEndpoint = TestUtils.getPropertyValue(scatterGather, "gatherEndpoint", Lifecycle.class);
    assertNotNull(TestUtils.getPropertyValue(scatterGather, "gatherEndpoint"));
    assertThat(TestUtils.getPropertyValue(scatterGather, "gatherEndpoint"), instanceOf(EventDrivenConsumer.class));
    assertTrue(TestUtils.getPropertyValue(scatterGather, "gatherEndpoint.running", Boolean.class));
    assertEquals(100L, TestUtils.getPropertyValue(scatterGather, "gatherTimeout"));
    assertTrue(this.beanFactory.containsBean("myGatherer"));
    Object gatherer = this.beanFactory.getBean("myGatherer");
    assertSame(gatherer, TestUtils.getPropertyValue(scatterGather, "gatherer"));
    assertSame(this.beanFactory.getBean("messageStore"), TestUtils.getPropertyValue(gatherer, "messageStore"));
    assertSame(gatherer, TestUtils.getPropertyValue(scatterGather, "gatherEndpoint.handler"));
    assertTrue(this.beanFactory.containsBean("myScatterer"));
    Object scatterer = this.beanFactory.getBean("myScatterer");
    assertTrue(TestUtils.getPropertyValue(scatterer, "applySequence", Boolean.class));
    Collection<RecipientListRouter.Recipient> recipients = TestUtils.getPropertyValue(scatterer, "recipients", Collection.class);
    assertEquals(1, recipients.size());
    assertSame(this.beanFactory.getBean("distributionChannel"), recipients.iterator().next().getChannel());
    Object scatterChannel = TestUtils.getPropertyValue(scatterGather, "scatterChannel");
    assertThat(scatterChannel, instanceOf(FixedSubscriberChannel.class));
    assertSame(scatterer, TestUtils.getPropertyValue(scatterChannel, "handler"));
    assertTrue(gatherEndpoint.isRunning());
    ((Lifecycle) scatterGather).stop();
    assertFalse(((Lifecycle) scatterGather).isRunning());
    assertFalse(gatherEndpoint.isRunning());
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) AggregatingMessageHandler(org.springframework.integration.aggregator.AggregatingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) Lifecycle(org.springframework.context.Lifecycle) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) Test(org.junit.Test)

Example 5 with Lifecycle

use of org.springframework.context.Lifecycle in project spring-cloud-gateway by spring-cloud.

the class WebSocketIntegrationTests method setup.

@Before
public void setup() throws Exception {
    this.client = new ReactorNettyWebSocketClient();
    this.server = new ReactorHttpServer();
    this.server.setHandler(createHttpHandler());
    this.server.afterPropertiesSet();
    this.server.start();
    // Set dynamically chosen port
    this.serverPort = this.server.getPort();
    if (this.client instanceof Lifecycle) {
        ((Lifecycle) this.client).start();
    }
    this.gatewayContext = new SpringApplicationBuilder(GatewayConfig.class).properties("ws.server.port:" + this.serverPort, "server.port=0", "spring.jmx.enabled=false").run();
    ConfigurableEnvironment env = this.gatewayContext.getBean(ConfigurableEnvironment.class);
    this.gatewayPort = new Integer(env.getProperty("local.server.port"));
}
Also used : ReactorHttpServer(org.springframework.cloud.gateway.test.support.ReactorHttpServer) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Lifecycle(org.springframework.context.Lifecycle) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Before(org.junit.Before)

Aggregations

Lifecycle (org.springframework.context.Lifecycle)23 SmartLifecycle (org.springframework.context.SmartLifecycle)6 MessageChannel (org.springframework.messaging.MessageChannel)5 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 MessageProducer (org.springframework.integration.core.MessageProducer)3 MessageHandler (org.springframework.messaging.MessageHandler)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JMException (javax.management.JMException)2 Before (org.junit.Before)2 Test (org.junit.jupiter.api.Test)2 TargetSource (org.springframework.aop.TargetSource)2 Advised (org.springframework.aop.framework.Advised)2 BeansException (org.springframework.beans.BeansException)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 DisposableBean (org.springframework.beans.factory.DisposableBean)2