Skip to content

Commit

Permalink
MOD: ThriftServerConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
coder4 committed Aug 31, 2017
1 parent 28db729 commit ee996ef
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ targetCompatibility = 1.8
// Use as maven groupId/artifactId/version
group = 'com.coder4.sbmvt'
project.archivesBaseName = 'sbmvt-thrift-server'
project.version = '0.0.4'
project.version = '0.0.5'

// source & javadoc pom
task sourcesJar(type: Jar, dependsOn: classes) {
Expand All @@ -25,6 +25,7 @@ repositories {
}

dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-eureka:1.3.1.RELEASE'
compileOnly 'org.apache.thrift:libthrift:0.9.3'

testCompileOnly 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @(#)ThriftServerRunnable.java, Jul 31, 2017.
* <p>
* Copyright 2017 coder4.com. All rights reserved.
* CODER4.COM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.coder4.sbmvt.thrift.server;

import com.netflix.discovery.EurekaClient;
import org.apache.thrift.TProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
* @author coder4
*/
@Configuration
@ConditionalOnBean(value = {TProcessor.class, EurekaClient.class})
public class ThriftServerConfiguration implements InitializingBean, DisposableBean {

private Logger LOG = LoggerFactory.getLogger(ThriftServerConfiguration.class);

@Autowired
private TProcessor processor;

@Autowired
private EurekaClient eurekaClient;

private ThriftServerRunnable thriftServer;

private Thread thread;

@Override
public void destroy() throws Exception {
// Unregister from eureka server & Sleep for 6 seconds
// current has bug, have to try catch
// https://github.com/spring-cloud/spring-cloud-netflix/issues/2099
try {
LOG.info("ThriftServerConfiguration destroy, shutdown eureka client.");
eurekaClient.shutdown();
} catch (Exception e) {
LOG.error("eurekaClient shutdown exception", e);
}

Thread.sleep(TimeUnit.SECONDS.toMillis(6));
LOG.info("ThriftServerConfiguration destroy, shutdown rpc server.");
thriftServer.stop();
}

@Override
public void afterPropertiesSet() throws Exception {

thriftServer = new ThriftServerRunnable(processor);

thread = new Thread(thriftServer);
thread.start();
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.coder4.sbmvt.thrift.server.ThriftServerConfiguration

0 comments on commit ee996ef

Please sign in to comment.