BeanConfig.java
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.crossoverjie.cim.client.config;
import com.crossoverjie.cim.client.handle.MsgHandleCaller;
import com.crossoverjie.cim.client.service.impl.MsgCallBackListener;
import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.data.construct.RingBufferWheel;
import com.crossoverjie.cim.common.protocol.CIMRequestProto;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.*;
/**
* Function:bean 配置
*
* @author crossoverJie
* Date: 24/05/2018 15:55
* @since JDK 1.8
*/
@Configuration
public class BeanConfig {
private final static Logger LOGGER = LoggerFactory.getLogger(BeanConfig.class);
@Value("${cim.user.id}")
private long userId;
@Value("${cim.callback.thread.queue.size}")
private int queueSize;
@Value("${cim.callback.thread.pool.size}")
private int poolSize;
/**
* 创建心跳单例
* @return
*/
@Bean(value = "heartBeat")
public CIMRequestProto.CIMReqProtocol heartBeat() {
CIMRequestProto.CIMReqProtocol heart = CIMRequestProto.CIMReqProtocol.newBuilder()
.setRequestId(userId)
.setReqMsg("ping")
.setType(Constants.CommandType.PING)
.build();
return heart;
}
/**
* http client
* @return okHttp
*/
@Bean
public OkHttpClient okHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.retryOnConnectionFailure(true);
return builder.build();
}
/**
* 创建回调线程池
* @return
*/
@Bean("callBackThreadPool")
public ThreadPoolExecutor buildCallerThread(){
BlockingQueue<Runnable> queue = new LinkedBlockingQueue(queueSize);
ThreadFactory product = new ThreadFactoryBuilder()
.setNameFormat("msg-callback-%d")
.setDaemon(true)
.build();
ThreadPoolExecutor productExecutor = new ThreadPoolExecutor(poolSize, poolSize, 1, TimeUnit.MILLISECONDS, queue,product);
return productExecutor ;
}
@Bean("scheduledTask")
public ScheduledExecutorService buildSchedule(){
ThreadFactory sche = new ThreadFactoryBuilder()
.setNameFormat("reConnect-job-%d")
.setDaemon(true)
.build();
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,sche) ;
return scheduledExecutorService ;
}
/**
* 回调 bean
* @return
*/
@Bean
public MsgHandleCaller buildCaller(){
MsgHandleCaller caller = new MsgHandleCaller(new MsgCallBackListener()) ;
return caller ;
}
@Bean
public RingBufferWheel bufferWheel(){
ExecutorService executorService = Executors.newFixedThreadPool(2) ;
return new RingBufferWheel(executorService) ;
}
}