Browse Source

brokerserver退出事件

Airhead 8 years ago
parent
commit
479e833eaf

+ 10 - 3
hos-broker/src/main/java/com/yihu/hos/broker/HosBrokerApplication.java

@ -14,10 +14,9 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class HosBrokerApplication extends SpringBootServletInitializer implements CommandLineRunner {
    @Autowired
    private GridFsOperations operations;
    private CamelStartBoot camelStartBoot;
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(HosBrokerApplication.class);
@ -27,7 +26,15 @@ public class HosBrokerApplication extends SpringBootServletInitializer implement
    @Override
    public void run(String... strings) throws Exception {
        GridFSUtil.gridFsOperations = operations;
        new CamelStartBoot().start();
        camelStartBoot = new CamelStartBoot();
        camelStartBoot.start();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                camelStartBoot.shutdown();
            } catch (Exception ignored) {
            }
        }));
    }
    @Override

+ 2 - 2
hos-broker/src/main/java/com/yihu/hos/broker/common/camelrouter/BrokerServerRouter.java

@ -16,8 +16,8 @@ public class BrokerServerRouter extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        if (arbiterConfiguration.getEnable()) {
            from("timer:brokerServerOnline?period={{hos.timer.period}}")
                    .to("bean:brokerServerService?method=brokerServerOnline");
            from("timer:online?period={{hos.timer.period}}")
                    .to("bean:brokerServerService?method=online");
        }
    }
}

+ 3 - 9
hos-broker/src/main/java/com/yihu/hos/broker/services/BrokerServerService.java

@ -13,7 +13,6 @@ import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -28,7 +27,7 @@ import java.util.Map;
 * @created Airhead 2016/8/1.
 */
@Component("brokerServerService")
public class BrokerServerService implements DisposableBean {
public class BrokerServerService {
    private static boolean registered = false;
    @Autowired
    private ApplicationConfiguration applicationConfiguration;
@ -41,7 +40,7 @@ public class BrokerServerService implements DisposableBean {
    private String hostAddress;
    private int port;
    public void brokerServerOnline() {
    public void online() {
        try {
            hostName = InetAddressUtil.getLocalHostName();
            hostAddress = IPChoiceUtils.getSourceIP();
@ -71,7 +70,7 @@ public class BrokerServerService implements DisposableBean {
    /**
     * brokerServer下线时通知
     */
    public void brokerServerOffline() {
    public void offline() {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode objectNode = objectMapper.createObjectNode();
@ -95,9 +94,4 @@ public class BrokerServerService implements DisposableBean {
        }
    }
    @Override
    public void destroy() throws Exception {
//        this.brokerServerOffline();
    }
}

+ 15 - 0
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/CamelStartBoot.java

@ -1,8 +1,10 @@
package com.yihu.hos.broker.services.camel;
import com.yihu.hos.broker.services.BrokerServerService;
import org.apache.camel.builder.RouteBuilder;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import java.net.URL;
import java.net.URLClassLoader;
@ -17,6 +19,13 @@ import java.util.concurrent.SynchronousQueue;
public class CamelStartBoot {
    private static Logger logger = LogManager.getLogger(ESBCamelService.class);
    private BrokerServerService brokerServerService;
    @Autowired
    public void setBrokerServerService(BrokerServerService brokerServerService) {
        this.brokerServerService = brokerServerService;
    }
    public void start() {
        logger.info("Apache Camel Context 启动...");
@ -42,10 +51,16 @@ public class CamelStartBoot {
                    SystemCamelContext.getContext().addRoutes(routeBuilder);
                }
            }
            brokerServerService.online();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Apache Camel Context 启动失败...");
        }
    }
    public void shutdown() {
        brokerServerService.offline();
    }
}

+ 3 - 3
hos-broker/src/main/resources/application.yml

@ -115,9 +115,9 @@ spring:
  profiles: lfq
  datasource:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://172.19.103.51:3306/db1?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
      username: canal
      password: canal
      url: jdbc:mysql://172.19.103.57:3306/db1?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
      username: esb
      password: esb
      test-on-borrow: true
      validation-query: SELECT 1
      test-while-idle: true

+ 8 - 1
src/main/resources/application.yml

@ -141,4 +141,11 @@ spring:
    password: admin
    pooled: false
  aop:
    proxy-target-class: true
    proxy-target-class: true
hos:
  zbus:
    url: 192.168.131.119:15555
    port: 15555
    store: ./store
  mysql:
    filePath: e://learn.sql   #租户基础表 sql文件位置