-
Notifications
You must be signed in to change notification settings - Fork 507
/
NettyRpcReference.java
95 lines (82 loc) · 2.88 KB
/
NettyRpcReference.java
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
/**
* Copyright (C) 2016 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.spring;
import com.google.common.eventbus.EventBus;
import com.newlandframework.rpc.event.ClientStopEvent;
import com.newlandframework.rpc.event.ClientStopEventListener;
import com.newlandframework.rpc.netty.MessageSendExecutor;
import com.newlandframework.rpc.serialize.RpcSerializeProtocol;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
/**
* @author tangjie<https://github.com/tang-jie>
* @filename:NettyRpcReference.java
* @description:NettyRpcReference功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2016/10/7
*/
public class NettyRpcReference implements FactoryBean, InitializingBean, DisposableBean {
private String interfaceName;
private String ipAddr;
private String protocol;
private EventBus eventBus = new EventBus();
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public String getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
@Override
public void destroy() throws Exception {
eventBus.post(new ClientStopEvent(0));
}
@Override
public void afterPropertiesSet() throws Exception {
MessageSendExecutor.getInstance().setRpcServerLoader(ipAddr, RpcSerializeProtocol.valueOf(protocol));
ClientStopEventListener listener = new ClientStopEventListener();
eventBus.register(listener);
}
@Override
public Object getObject() throws Exception {
return MessageSendExecutor.getInstance().execute(getObjectType());
}
@Override
public Class<?> getObjectType() {
try {
return this.getClass().getClassLoader().loadClass(interfaceName);
} catch (ClassNotFoundException e) {
System.err.println("spring analyze fail!");
}
return null;
}
@Override
public boolean isSingleton() {
return true;
}
}