Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,23 @@

package org.apache.servicecomb.core;

import java.util.Collection;

import javax.inject.Inject;

import org.apache.servicecomb.core.BootListener.BootEvent;
import org.apache.servicecomb.core.BootListener.EventType;
import org.apache.servicecomb.core.definition.loader.SchemaListenerManager;
import org.apache.servicecomb.core.endpoint.AbstractEndpointsCache;
import org.apache.servicecomb.core.handler.HandlerConfigUtils;
import org.apache.servicecomb.core.provider.consumer.ConsumerProviderManager;
import org.apache.servicecomb.core.provider.consumer.ReferenceConfigUtils;
import org.apache.servicecomb.core.provider.producer.ProducerProviderManager;
import org.apache.servicecomb.core.transport.TransportManager;
import org.apache.servicecomb.foundation.common.event.EventManager;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.FortifyUtils;
import org.apache.servicecomb.serviceregistry.RegistryUtils;
import org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;

import com.google.common.eventbus.Subscribe;

public class CseApplicationListener
implements ApplicationListener<ApplicationEvent>, Ordered, ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(CseApplicationListener.class);

private static boolean isInit = false;

@Inject
private ProducerProviderManager producerProviderManager;

@Inject
private ConsumerProviderManager consumerProviderManager;

@Inject
private TransportManager transportManager;

@Inject
private SchemaListenerManager schemaListenerManager;

private Collection<BootListener> bootListenerList;

private Class<?> initEventClass = ContextRefreshedEvent.class;

private ApplicationContext applicationContext;
Expand All @@ -91,92 +55,22 @@ public int getOrder() {
return -1000;
}

protected void triggerEvent(EventType eventType) {
BootEvent event = new BootEvent();
event.setEventType(eventType);

for (BootListener listener : bootListenerList) {
listener.onBootEvent(event);
}
}

@Override
public void onApplicationEvent(ApplicationEvent event) {
if (initEventClass.isInstance(event)) {
//TODO to load when webapplication context is used for discovery client, need to check if can use the order and undo this change with proper fix.
if (!isInit) {
try {
bootListenerList = applicationContext.getBeansOfType(BootListener.class).values();

AbstractEndpointsCache.init(RegistryUtils.getInstanceCacheManager(), transportManager);

triggerEvent(EventType.BEFORE_HANDLER);
HandlerConfigUtils.init();
triggerEvent(EventType.AFTER_HANDLER);

triggerEvent(EventType.BEFORE_PRODUCER_PROVIDER);
producerProviderManager.init();
triggerEvent(EventType.AFTER_PRODUCER_PROVIDER);

triggerEvent(EventType.BEFORE_CONSUMER_PROVIDER);
consumerProviderManager.init();
triggerEvent(EventType.AFTER_CONSUMER_PROVIDER);

triggerEvent(EventType.BEFORE_TRANSPORT);
transportManager.init();
triggerEvent(EventType.AFTER_TRANSPORT);

schemaListenerManager.notifySchemaListener();

triggerEvent(EventType.BEFORE_REGISTRY);

triggerAfterRegistryEvent();

RegistryUtils.run();

// 当程序退出时,进行相关清理,注意:kill -9 {pid}下无效
// 1. 去注册实例信息
// TODO 服务优雅退出
if (applicationContext instanceof AbstractApplicationContext) {
((AbstractApplicationContext) applicationContext).registerShutdownHook();
}
isInit = true;
} catch (Exception e) {
LOGGER.error("cse init failed, {}", FortifyUtils.getErrorInfo(e));
}
if (applicationContext instanceof AbstractApplicationContext) {
((AbstractApplicationContext) applicationContext).registerShutdownHook();
}
} else if (event instanceof ContextClosedEvent) {
LOGGER.warn("cse is closing now...");
triggerEvent(EventType.BEFORE_CLOSE);
RegistryUtils.destroy();
triggerEvent(EventType.AFTER_CLOSE);
isInit = false;
}
}

/**
* <p>As the process of instance registry is asynchronous, the {@code AFTER_REGISTRY}
* event should not be sent immediately after {@link RegistryUtils#run()} is invoked.
* When the instance registry succeeds, {@link MicroserviceInstanceRegisterTask} will be posted in {@link EventManager},
* register a subscriber to watch this event and send {@code AFTER_REGISTRY}.</p>
*
* <p>This method should be called before {@link RegistryUtils#run()} to avoid that the registry process is too quick
* that the event is not watched by this subscriber.</p>
*
* <p>Check if {@code InstanceId} is null to judge whether the instance registry has succeeded.</p>
*/
private void triggerAfterRegistryEvent() {
EventManager.register(new Object() {
@Subscribe
public void afterRegistryInstance(MicroserviceInstanceRegisterTask microserviceInstanceRegisterTask) {
LOGGER.info("receive MicroserviceInstanceRegisterTask event, check instance Id...");
if (!StringUtils.isEmpty(RegistryUtils.getMicroserviceInstance().getInstanceId())) {
LOGGER.info("instance registry succeeds for the first time, will send AFTER_REGISTRY event.");
ReferenceConfigUtils.setReady(true);
triggerEvent(EventType.AFTER_REGISTRY);
EventManager.unregister(this);
}
if (SCBEngine.getInstance().getBootListenerList() == null) {
SCBEngine.getInstance().setProducerProviderManager(applicationContext.getBean(ProducerProviderManager.class));
SCBEngine.getInstance().setConsumerProviderManager(applicationContext.getBean(ConsumerProviderManager.class));
SCBEngine.getInstance().setTransportManager(applicationContext.getBean(TransportManager.class));
SCBEngine.getInstance().setSchemaListenerManager(applicationContext.getBean(SchemaListenerManager.class));
SCBEngine.getInstance().setBootListenerList(applicationContext.getBeansOfType(BootListener.class).values());
}
});

SCBEngine.getInstance().init();
}
}
}
Loading