├── .gitignore
├── EasyRest
├── pom.xml
└── src
│ └── main
│ ├── java
│ ├── META-INF
│ │ └── MANIFEST.MF
│ └── tech
│ │ └── dbgsoftware
│ │ └── easyrest
│ │ ├── EasyRest.java
│ │ ├── EasyRestCallback.java
│ │ ├── actors
│ │ ├── ActorFactory.java
│ │ ├── AnalysisMethodActor.java
│ │ ├── EasyRestAkkaConf.java
│ │ ├── ExceptionHandleActor.java
│ │ ├── Signal.java
│ │ ├── remote
│ │ │ ├── RemoteInvokeActor.java
│ │ │ ├── RemoteObjectAnalysisActor.java
│ │ │ ├── RemoteRequestUtil.java
│ │ │ ├── RemoteServiceExchangeActor.java
│ │ │ ├── conf
│ │ │ │ ├── EasyRestDistributedServiceBind.java
│ │ │ │ └── ServiceMapping.java
│ │ │ └── model
│ │ │ │ ├── RemoteInvokeObject.java
│ │ │ │ └── ServiceInfo.java
│ │ ├── request
│ │ │ ├── ControllerInvokeActor.java
│ │ │ └── RequestProcessActor.java
│ │ └── response
│ │ │ ├── OutputActor.java
│ │ │ └── ResponseProcessActor.java
│ │ ├── annotations
│ │ ├── async
│ │ │ ├── AsyncRequest.java
│ │ │ └── SyncRequest.java
│ │ ├── bean
│ │ │ ├── BindService.java
│ │ │ └── EasyRestInterface.java
│ │ ├── history
│ │ │ └── HistoryRequired.java
│ │ ├── method
│ │ │ ├── BindURL.java
│ │ │ ├── Delete.java
│ │ │ ├── Get.java
│ │ │ ├── Post.java
│ │ │ ├── Put.java
│ │ │ ├── RequestMethod.java
│ │ │ ├── SkipCustomerInject.java
│ │ │ └── SkipCustomerResponse.java
│ │ ├── parameter
│ │ │ ├── AllDefined.java
│ │ │ ├── AutoInject.java
│ │ │ ├── FromPath.java
│ │ │ ├── NotNull.java
│ │ │ ├── Optional.java
│ │ │ ├── SkipInject.java
│ │ │ └── validation
│ │ │ │ ├── AllDefinedValidation.java
│ │ │ │ └── NotNullValidation.java
│ │ ├── permission
│ │ │ └── PermissionRequired.java
│ │ └── transaction
│ │ │ └── TransactionRequired.java
│ │ ├── aop
│ │ ├── AopPostCommitStep.java
│ │ ├── AopPreCommitStep.java
│ │ ├── AopStep.java
│ │ ├── StaticAopStepUtil.java
│ │ ├── customer
│ │ │ ├── CustomInjection.java
│ │ │ └── CustomResponse.java
│ │ ├── pre
│ │ │ ├── AopInitRequestInfoStep.java
│ │ │ ├── AopParametersInjectStep.java
│ │ │ └── AopRequestValidateStep.java
│ │ └── resolvers
│ │ │ ├── FormDataResolve.java
│ │ │ ├── JsonDataResolve.java
│ │ │ ├── ParameterTypeResolve.java
│ │ │ └── UrlDataResolve.java
│ │ ├── exception
│ │ ├── ConditionMissingException.java
│ │ ├── EasyRestException.java
│ │ ├── ExceptionHandler.java
│ │ ├── MethodNotAllowedException.java
│ │ ├── PageNotFoundException.java
│ │ ├── ParameterNotFoundException.java
│ │ └── PermissionException.java
│ │ ├── ioc
│ │ ├── remote
│ │ │ ├── EasyRestProxyFactory.java
│ │ │ ├── EasyRestServiceLookup.java
│ │ │ └── proxy
│ │ │ │ ├── ProxyForBeanClassInvoke.java
│ │ │ │ └── ProxyForBeanNameInvoke.java
│ │ └── utils
│ │ │ ├── BeanOperationUtils.java
│ │ │ └── StaticClassUtils.java
│ │ ├── model
│ │ ├── ErrorResponse.java
│ │ ├── HttpEntity.java
│ │ ├── Response.java
│ │ ├── ResponseEntity.java
│ │ └── request
│ │ │ ├── AbstractRequestModel.java
│ │ │ ├── Request.java
│ │ │ ├── RequestModel.java
│ │ │ └── RestObject.java
│ │ ├── network
│ │ ├── NettyInit.java
│ │ ├── NettyLaunch.java
│ │ ├── conf
│ │ │ └── ChannelOptionBuilder.java
│ │ ├── core
│ │ │ ├── api
│ │ │ │ └── BaseConfiguration.java
│ │ │ └── pipeline
│ │ │ │ ├── in
│ │ │ │ └── RequestProcessHandler.java
│ │ │ │ └── utils
│ │ │ │ └── ByteBufUtils.java
│ │ ├── exception
│ │ │ └── ConfigurationException.java
│ │ └── router
│ │ │ ├── PathNode.java
│ │ │ ├── PathStore.java
│ │ │ ├── RouterProvider.java
│ │ │ └── UrlFormat.java
│ │ └── utils
│ │ ├── DateUtils.java
│ │ ├── DoubleTypeAdapter.java
│ │ ├── DoubleUtils.java
│ │ ├── IntegerTypeAdapter.java
│ │ ├── IntegerUtils.java
│ │ ├── JsonTranslationUtil.java
│ │ ├── LogUtils.java
│ │ ├── LongTypeAdapter.java
│ │ ├── StringUtils.java
│ │ └── ThreadStackUtils.java
│ └── resources
│ └── easyrest-applicationContext-01.xml
├── Example
├── Example-Distributed-Service-1
│ ├── example-service-1-api
│ │ ├── pom.xml
│ │ └── src
│ │ │ └── main
│ │ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── api
│ │ │ └── Service1.java
│ ├── example-service-1-main
│ │ ├── pom.xml
│ │ └── src
│ │ │ └── main
│ │ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── main
│ │ │ │ ├── Startup.java
│ │ │ │ └── impl
│ │ │ │ └── Service1Impl.java
│ │ │ └── resources
│ │ │ ├── MyExampleApplicationContext-01.xml
│ │ │ ├── application.conf
│ │ │ ├── log4j.properties
│ │ │ └── services-mapping-01.json
│ └── pom.xml
├── Example-Distributed-Service-2
│ ├── example-service-2-api
│ │ ├── pom.xml
│ │ └── src
│ │ │ └── main
│ │ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── api
│ │ │ └── Service2.java
│ ├── example-service-2-main
│ │ ├── pom.xml
│ │ └── src
│ │ │ └── main
│ │ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── main
│ │ │ │ ├── Startup.java
│ │ │ │ └── impl
│ │ │ │ └── Service2Impl.java
│ │ │ └── resources
│ │ │ ├── MyExampleApplicationContext-02.xml
│ │ │ ├── application.conf
│ │ │ ├── log4j.properties
│ │ │ └── services-mapping-02.json
│ └── pom.xml
├── Example-Distributed-Service-Model
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── model
│ │ └── People.java
├── Example-Quick-Start
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ ├── Example.java
│ │ │ ├── InfoMation.java
│ │ │ ├── model
│ │ │ └── Stock.java
│ │ │ ├── rest
│ │ │ ├── StockInfoRest.java
│ │ │ └── TestRestService.java
│ │ │ └── services
│ │ │ ├── StockInfoRestServiceImpl.java
│ │ │ └── TestServiceImpl.java
│ │ └── resources
│ │ ├── MyExampleApplicationContext.xml
│ │ └── log4j.properties
└── pom.xml
├── README-zh.md
├── README.md
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | *.war
3 | *.jar
4 | *.iml
5 | .idea/
6 | out/
7 | target/
--------------------------------------------------------------------------------
/EasyRest/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tech.dbgsoftware.easyrest
8 | EasyRest-NAS-All-In-One
9 | 0.0.1
10 |
11 |
12 | org.sonatype.oss
13 | oss-parent
14 | 7
15 |
16 |
17 |
18 | UTF-8
19 | UTF-8
20 |
21 |
22 |
23 |
24 | io.netty
25 | netty-all
26 | 4.1.36.Final
27 |
28 |
29 | com.typesafe.akka
30 | akka-actor_2.12
31 | 2.5.12
32 |
33 |
34 | com.typesafe.akka
35 | akka-cluster_2.12
36 | 2.5.12
37 |
38 |
39 | com.typesafe.akka
40 | akka-remote_2.12
41 | 2.5.12
42 |
43 |
44 | org.springframework
45 | spring-webmvc
46 | 4.1.6.RELEASE
47 |
48 |
49 | com.google.code.gson
50 | gson
51 | 2.3.1
52 |
53 |
54 | org.javassist
55 | javassist
56 | 3.22.0-GA
57 |
58 |
59 | com.google.guava
60 | guava
61 | 19.0
62 |
63 |
64 | log4j
65 | log4j
66 | 1.2.17
67 |
68 |
69 | org.slf4j
70 | slf4j-api
71 | 1.7.21
72 |
73 |
74 | org.slf4j
75 | slf4j-simple
76 | 1.7.21
77 |
78 |
79 |
80 |
81 |
82 | The Apache Software License, Version 2.0
83 | http://www.apache.org/licenses/LICENSE-2.0.txt
84 | repo
85 |
86 |
87 |
88 |
89 | https://github.com/liuhongyuand/EasyRest-NAS
90 | git@github.com:liuhongyuand/EasyRest-NAS.git
91 | liuhongyu.louie@gmail.com
92 |
93 |
94 |
95 |
96 | Louie
97 | liuhongyu.louie@gmail.com
98 | liuhongyu.louie@gmail.com
99 |
100 |
101 |
102 |
103 |
104 | release
105 |
106 |
107 |
108 |
109 | org.apache.maven.plugins
110 | maven-source-plugin
111 | 2.2.1
112 |
113 |
114 | package
115 |
116 | jar-no-fork
117 |
118 |
119 |
120 |
121 |
122 |
123 | org.apache.maven.plugins
124 | maven-javadoc-plugin
125 | 2.9.1
126 |
127 |
128 | package
129 |
130 | jar
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 | org.apache.maven.plugins
139 | maven-gpg-plugin
140 | 1.6
141 |
142 |
143 | verify
144 |
145 | sign
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 | sonatype-nexus-snapshots
155 | https://oss.sonatype.org/content/repositories/snapshots
156 |
157 |
158 | sonatype-nexus-staging
159 | https://oss.sonatype.org/service/local/staging/deploy/maven2
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 | org.apache.maven.plugins
169 | maven-compiler-plugin
170 | 2.0.2
171 |
172 | 1.8
173 | 1.8
174 | ${project.build.sourceEncoding}
175 |
176 |
177 |
178 | org.apache.maven.plugins
179 | maven-shade-plugin
180 | 1.4
181 |
182 |
183 | package
184 |
185 | shade
186 |
187 |
188 | false
189 | true
190 | ALL
191 |
192 |
193 | *:*
194 |
195 |
196 |
197 |
199 | META-INF/spring.handlers
200 |
201 |
203 | META-INF/spring.schemas
204 |
205 |
207 | reference.conf
208 |
209 |
211 |
212 | akka.Main
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Class-Path: spring-webmvc-4.1.6.RELEASE.jar akka-stream_2.12-2.5.12.ja
3 | r aopalliance-1.0.jar scala-java8-compat_2.12-0.8.0.jar aeron-driver-
4 | 1.7.0.jar guava-19.0.jar agrona-0.9.12.jar spring-expression-4.1.6.RE
5 | LEASE.jar akka-remote_2.12-2.5.12.jar spring-context-4.1.6.RELEASE.ja
6 | r gson-2.3.1.jar spring-aop-4.1.6.RELEASE.jar slf4j-api-1.7.21.jar sp
7 | ring-beans-4.1.6.RELEASE.jar akka-actor_2.12-2.5.12.jar reactive-stre
8 | ams-1.0.2.jar config-1.3.2.jar spring-core-4.1.6.RELEASE.jar slf4j-si
9 | mple-1.7.21.jar commons-logging-1.2.jar netty-3.10.6.Final.jar log4j-
10 | 1.2.17.jar scala-library-2.12.5.jar javassist-3.22.0-GA.jar scala-par
11 | ser-combinators_2.12-1.1.0.jar ssl-config-core_2.12-0.2.3.jar akka-pr
12 | otobuf_2.12-2.5.12.jar spring-web-4.1.6.RELEASE.jar aeron-client-1.7.
13 | 0.jar netty-all-4.1.22.Final.jar akka-cluster_2.12-2.5.12.jar
14 |
15 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/EasyRest.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest;
2 |
3 | import akka.actor.ActorRef;
4 | import org.springframework.context.support.ClassPathXmlApplicationContext;
5 | import tech.dbgsoftware.easyrest.actors.ActorFactory;
6 | import tech.dbgsoftware.easyrest.actors.AnalysisMethodActor;
7 | import tech.dbgsoftware.easyrest.ioc.utils.BeanOperationUtils;
8 | import tech.dbgsoftware.easyrest.network.NettyInit;
9 |
10 | public class EasyRest {
11 |
12 | private EasyRestCallback easyRestCallback;
13 |
14 | private String systemName;
15 |
16 | private NettyInit nettyInit;
17 |
18 | public EasyRest() {
19 | BeanOperationUtils.setApplicationContext(new ClassPathXmlApplicationContext("classpath:easyrest-applicationContext-01.xml"));
20 | }
21 |
22 | public EasyRest(String springXmlPath) {
23 | String[] xmls = new String[]{"classpath:easyrest-applicationContext-01.xml", springXmlPath};
24 | BeanOperationUtils.setApplicationContext(new ClassPathXmlApplicationContext(xmls));
25 | }
26 |
27 | public EasyRest(String... springXmlPaths) {
28 | String[] xmls = new String[springXmlPaths.length + 1];
29 | xmls[0] = "classpath:easyrest-applicationContext-01.xml";
30 | System.arraycopy(springXmlPaths, 0, xmls, 1, xmls.length - 1);
31 | BeanOperationUtils.setApplicationContext(new ClassPathXmlApplicationContext(xmls));
32 | }
33 |
34 | public void startup() {
35 | startup(NettyInit.SystemName, new NettyInit());
36 | }
37 |
38 | public void startup(EasyRestCallback easyRestCallback) {
39 | startup(NettyInit.SystemName, new NettyInit(), easyRestCallback);
40 | }
41 |
42 | public void startup(String systemName) {
43 | startup(systemName, new NettyInit());
44 | }
45 |
46 | public void startup(String systemName, EasyRestCallback easyRestCallback) {
47 | startup(systemName, new NettyInit(), easyRestCallback);
48 | }
49 |
50 | public void startup(String systemName, int port) {
51 | startup(systemName, new NettyInit(port));
52 | }
53 |
54 | public void startup(String systemName, int port, EasyRestCallback easyRestCallback) {
55 | startup(systemName, new NettyInit(port), easyRestCallback);
56 | }
57 |
58 | public void startup(String SystemName, NettyInit nettyInit) {
59 | startup(SystemName, nettyInit, null);
60 | }
61 |
62 | public void startup(String systemName, NettyInit nettyInit, EasyRestCallback easyRestCallback){
63 | this.systemName = systemName;
64 | this.nettyInit = nettyInit;
65 | NettyInit.SystemName = systemName;
66 | this.easyRestCallback = easyRestCallback;
67 | if (this.easyRestCallback == null) {
68 | this.easyRestCallback = new EasyRestCallback() {
69 | @Override
70 | public void onStartSuccess() {
71 |
72 | }
73 |
74 | @Override
75 | public void onStartFailed() {
76 |
77 | }
78 | };
79 | }
80 | ActorFactory.createActor(AnalysisMethodActor.class).tell(this, ActorRef.noSender());
81 | }
82 |
83 | public String getSystemName() {
84 | return systemName;
85 | }
86 |
87 | public NettyInit getNettyInit() {
88 | return nettyInit;
89 | }
90 |
91 | public EasyRestCallback getEasyRestCallback() {
92 | return easyRestCallback;
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/EasyRestCallback.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest;
2 |
3 | public interface EasyRestCallback {
4 |
5 | void onStartSuccess();
6 |
7 | void onStartFailed();
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/ActorFactory.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors;
2 |
3 | import akka.actor.ActorRef;
4 | import akka.actor.ActorSystem;
5 | import akka.actor.Props;
6 | import tech.dbgsoftware.easyrest.actors.remote.RemoteInvokeActor;
7 | import tech.dbgsoftware.easyrest.actors.remote.RemoteServiceExchangeActor;
8 | import tech.dbgsoftware.easyrest.network.NettyInit;
9 | import tech.dbgsoftware.easyrest.utils.LogUtils;
10 |
11 | import java.util.Map;
12 | import java.util.concurrent.ConcurrentHashMap;
13 | import java.util.concurrent.TimeUnit;
14 | import java.util.concurrent.locks.Lock;
15 | import java.util.concurrent.locks.ReentrantLock;
16 |
17 | public class ActorFactory {
18 |
19 | private static ActorSystem ACTOR_SYSTEM;
20 |
21 | private static final Map ACTOR_REF_CACHE = new ConcurrentHashMap<>();
22 |
23 | private static final Lock LOCK = new ReentrantLock();
24 |
25 | static {
26 | ACTOR_SYSTEM = ActorSystem.create(NettyInit.SystemName);
27 | createActorWithName(RemoteInvokeActor.class);
28 | createActorWithName(RemoteServiceExchangeActor.class);
29 | }
30 |
31 | public static ActorSystem getActorSystem() {
32 | return ACTOR_SYSTEM;
33 | }
34 |
35 | public static ActorRef createActor(Class target){
36 | try {
37 | if (!ACTOR_REF_CACHE.containsKey(target) && LOCK.tryLock(1, TimeUnit.MINUTES)){
38 | try {
39 | Props props = Props.create(target);
40 | ACTOR_REF_CACHE.putIfAbsent(target, ACTOR_SYSTEM.actorOf(props));
41 | } finally {
42 | LOCK.unlock();
43 | }
44 | }
45 | return ACTOR_REF_CACHE.get(target);
46 | } catch (InterruptedException e) {
47 | LogUtils.error(e.getMessage(), e);
48 | return ACTOR_SYSTEM.actorOf(Props.create(target));
49 | }
50 | }
51 |
52 | private static void createActorWithName(Class target){
53 | try {
54 | if (!ACTOR_REF_CACHE.containsKey(target) && LOCK.tryLock(1, TimeUnit.MINUTES)){
55 | try {
56 | Props props = Props.create(target);
57 | ACTOR_REF_CACHE.putIfAbsent(target, ACTOR_SYSTEM.actorOf(props, target.getSimpleName()));
58 | } finally {
59 | LOCK.unlock();
60 | }
61 | }
62 | } catch (InterruptedException e) {
63 | LogUtils.error(e.getMessage(), e);
64 | }
65 | }
66 |
67 | public static ActorRef createRemoteServiceExchangedActor(String systemName, String host, String port){
68 | return ACTOR_SYSTEM.actorFor(String.format("akka.tcp://%s@%s:%s/user/RemoteServiceExchangeActor", systemName, host, port));
69 | }
70 |
71 | public static ActorRef createRemoteInvokeActor(String systemName, String host, String port){
72 | return ACTOR_SYSTEM.actorFor(String.format("akka.tcp://%s@%s:%s/user/RemoteInvokeActor", systemName, host, port));
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/AnalysisMethodActor.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors;
2 |
3 | import akka.actor.AbstractActor;
4 | import akka.actor.ActorRef;
5 | import io.netty.handler.codec.http.HttpMethod;
6 | import tech.dbgsoftware.easyrest.EasyRest;
7 | import tech.dbgsoftware.easyrest.actors.remote.conf.EasyRestDistributedServiceBind;
8 | import tech.dbgsoftware.easyrest.annotations.method.*;
9 | import tech.dbgsoftware.easyrest.ioc.utils.BeanOperationUtils;
10 | import tech.dbgsoftware.easyrest.network.NettyLaunch;
11 | import tech.dbgsoftware.easyrest.network.exception.ConfigurationException;
12 | import tech.dbgsoftware.easyrest.network.router.RouterProvider;
13 |
14 | import java.lang.reflect.Method;
15 | import java.util.ArrayList;
16 | import java.util.List;
17 |
18 | public class AnalysisMethodActor extends AbstractActor {
19 |
20 | @Override
21 | public Receive createReceive() {
22 | return receiveBuilder().match(EasyRest.class, (this::analysisRestObject)).build();
23 | }
24 |
25 | private void analysisRestObject(EasyRest easyRest) {
26 | List easyRestClass = new ArrayList<>();
27 | BeanOperationUtils.getAllBeansClass().forEach((aClass -> {
28 | for (Class _interface : aClass.getInterfaces()){
29 | if (_interface.isAnnotationPresent(BindURL.class)){
30 | easyRestClass.add(_interface);
31 | }
32 | }
33 | }));
34 | for (Class> requestModel : easyRestClass) {
35 | if (!requestModel.isInterface()) {
36 | throw new ConfigurationException("Only interface can be registered.");
37 | }
38 | Class controller = BeanOperationUtils.getBeansFromInterface(requestModel).getClass();
39 | StringBuffer[] restUri = new StringBuffer[1];
40 | if (requestModel.isAnnotationPresent(BindURL.class)) {
41 | String[] uris = requestModel.getAnnotation(BindURL.class).value();
42 | restUri = new StringBuffer[uris.length];
43 | for (int i = 0; i < restUri.length; i++) {
44 | restUri[i] = new StringBuffer(uris[i]);
45 | }
46 | }
47 | for (Method method : requestModel.getMethods()) {
48 | if (method.isAnnotationPresent(Post.class)) {
49 | RouterProvider.methodRouterResolve(restUri, method.getName(), method.getAnnotation(Post.class).value(), HttpMethod.POST, method, controller);
50 | }
51 | if (method.isAnnotationPresent(Get.class)) {
52 | RouterProvider.methodRouterResolve(restUri, method.getName(), method.getAnnotation(Get.class).value(), HttpMethod.GET, method, controller);
53 | }
54 | if (method.isAnnotationPresent(Put.class)) {
55 | RouterProvider.methodRouterResolve(restUri, method.getName(), method.getAnnotation(Put.class).value(), HttpMethod.PUT, method, controller);
56 | }
57 | if (method.isAnnotationPresent(Delete.class)) {
58 | RouterProvider.methodRouterResolve(restUri, method.getName(), method.getAnnotation(Delete.class).value(), HttpMethod.DELETE, method, controller);
59 | }
60 | }
61 | EasyRestDistributedServiceBind.addService(requestModel, controller);
62 | }
63 | ActorFactory.createActor(NettyLaunch.class).tell(easyRest, ActorRef.noSender());
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/EasyRestAkkaConf.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors;
2 |
3 | public class EasyRestAkkaConf {
4 |
5 | public static int REMOTE_PORT = 2550;
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/ExceptionHandleActor.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors;
2 |
3 | import akka.actor.AbstractActor;
4 | import tech.dbgsoftware.easyrest.model.ErrorResponse;
5 | import tech.dbgsoftware.easyrest.model.HttpEntity;
6 | import tech.dbgsoftware.easyrest.model.ResponseEntity;
7 |
8 | public class ExceptionHandleActor extends AbstractActor {
9 |
10 | @Override
11 | public Receive createReceive() {
12 | return receiveBuilder()
13 | .match(HttpEntity.class, (httpEntity -> {
14 | httpEntity.getResponse().buildResponse(ResponseEntity.buildFailedResponse(new ErrorResponse(httpEntity.getErrorMap())));
15 | httpEntity.getChannelHandlerContext().writeAndFlush(httpEntity.getResponse().getRealResponse());
16 | })).build();
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/Signal.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors;
2 |
3 | import com.google.gson.Gson;
4 | import com.google.gson.GsonBuilder;
5 | import tech.dbgsoftware.easyrest.model.ResponseEntity;
6 |
7 | public class Signal {
8 |
9 | private static final Gson GSON = new GsonBuilder().create();
10 |
11 | public static final String SUCCESS = "SUCCESS";
12 |
13 | public static final String FAILED = "FAILED";
14 |
15 | public static String getFailedMessage(){
16 | return GSON.toJson(ResponseEntity.buildFailedResponse());
17 | }
18 |
19 | public static String getSuccessMessage(){
20 | return GSON.toJson(ResponseEntity.buildOkResponse());
21 | }
22 |
23 | public static String getFailedMessage(String message){
24 | return GSON.toJson(ResponseEntity.buildFailedResponse(message));
25 | }
26 |
27 | public static String getSuccessMessage(String message){
28 | return GSON.toJson(ResponseEntity.buildOkResponse(message));
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/remote/RemoteInvokeActor.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors.remote;
2 |
3 | import akka.actor.AbstractActor;
4 | import akka.actor.ActorRef;
5 | import com.google.gson.Gson;
6 | import com.google.gson.GsonBuilder;
7 | import tech.dbgsoftware.easyrest.actors.ActorFactory;
8 | import tech.dbgsoftware.easyrest.actors.remote.model.RemoteInvokeObject;
9 |
10 | public class RemoteInvokeActor extends AbstractActor {
11 |
12 | private static final Gson GSON = new GsonBuilder().create();
13 |
14 | @Override
15 | public Receive createReceive() {
16 | return receiveBuilder().match(String.class, (jsonData)-> {
17 | RemoteInvokeObject remoteInvokeObject = GSON.fromJson(jsonData, RemoteInvokeObject.class);
18 | remoteInvokeObject.setSender(getSender());
19 | ActorFactory.createActor(RemoteObjectAnalysisActor.class).tell(remoteInvokeObject, ActorRef.noSender());
20 | }).build();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/remote/RemoteObjectAnalysisActor.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors.remote;
2 |
3 | import akka.actor.AbstractActor;
4 | import akka.actor.ActorRef;
5 | import tech.dbgsoftware.easyrest.actors.ActorFactory;
6 | import tech.dbgsoftware.easyrest.actors.Signal;
7 | import tech.dbgsoftware.easyrest.actors.remote.conf.EasyRestDistributedServiceBind;
8 | import tech.dbgsoftware.easyrest.actors.remote.model.RemoteInvokeObject;
9 | import tech.dbgsoftware.easyrest.actors.request.ControllerInvokeActor;
10 | import tech.dbgsoftware.easyrest.aop.resolvers.JsonDataResolve;
11 |
12 | import java.lang.reflect.Method;
13 |
14 | public class RemoteObjectAnalysisActor extends AbstractActor {
15 |
16 | @Override
17 | public Receive createReceive() {
18 | return receiveBuilder().match(RemoteInvokeObject.class, (remoteInvokeObject -> {
19 | Class controller = EasyRestDistributedServiceBind.getLocalServiceControllerMap().get(remoteInvokeObject.getInterfaceClassName());
20 | remoteInvokeObject.setImplClass(controller);
21 | Method invokeMethod = null;
22 | for (Method method : controller.getMethods()) {
23 | if (method.getName().equals(remoteInvokeObject.getMethodName())){
24 | invokeMethod = method;
25 | break;
26 | }
27 | }
28 | if (invokeMethod == null){
29 | remoteInvokeObject.getSender().tell(Signal.getFailedMessage(remoteInvokeObject.getMethodName() + " not found"), remoteInvokeObject.getSender());
30 | } else {
31 | if (invokeMethod.getGenericParameterTypes().length != remoteInvokeObject.getArgs().length){
32 | remoteInvokeObject.getSender().tell(Signal.getFailedMessage("parameter number not correct"), remoteInvokeObject.getSender());
33 | }
34 | remoteInvokeObject.setMethod(invokeMethod);
35 | remoteInvokeObject.updateArgs(JsonDataResolve.resolveArgs(invokeMethod.getGenericParameterTypes(), remoteInvokeObject.getArgs()));
36 | ActorFactory.createActor(ControllerInvokeActor.class).tell(remoteInvokeObject, ActorRef.noSender());
37 | }
38 | })).build();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/remote/RemoteRequestUtil.java:
--------------------------------------------------------------------------------
1 | package tech.dbgsoftware.easyrest.actors.remote;
2 |
3 | import akka.pattern.Patterns;
4 | import akka.util.Timeout;
5 | import scala.concurrent.Await;
6 | import scala.concurrent.Future;
7 | import scala.concurrent.duration.Duration;
8 | import tech.dbgsoftware.easyrest.actors.ActorFactory;
9 | import tech.dbgsoftware.easyrest.actors.remote.conf.EasyRestDistributedServiceBind;
10 | import tech.dbgsoftware.easyrest.actors.remote.model.RemoteInvokeObject;
11 | import tech.dbgsoftware.easyrest.actors.remote.model.ServiceInfo;
12 | import tech.dbgsoftware.easyrest.utils.JsonTranslationUtil;
13 | import tech.dbgsoftware.easyrest.utils.LogUtils;
14 |
15 | import java.lang.reflect.Method;
16 |
17 | public class RemoteRequestUtil {
18 |
19 | private static final Timeout REQUEST_TIMEOUT = new Timeout(Duration.create(60, "seconds"));
20 |
21 | private static final Timeout REQUEST_TIMEOUT_FOR_INIT = new Timeout(Duration.create(10, "seconds"));
22 |
23 | public static Object createRemoteRequest(Method method, Object[] args, long connectionMillis, long resultMillis){
24 | return createRemoteRequest(method, args, null, connectionMillis, resultMillis);
25 | }
26 |
27 | public static Object createRemoteRequest(Method method, Object[] args, String invokeBeanName, long connectionMillis, long resultMillis){
28 | ServiceInfo serviceInfo = EasyRestDistributedServiceBind.getServiceInfoMap().get(method.getDeclaringClass().getName());
29 | RemoteInvokeObject remoteInvokeObject;
30 | if (invokeBeanName != null) {
31 | remoteInvokeObject = new RemoteInvokeObject(method, args, invokeBeanName);
32 | } else {
33 | remoteInvokeObject = new RemoteInvokeObject(method, args);
34 | }
35 | return getInvoke(serviceInfo.getAkkaSystemName(), serviceInfo.getHost(), serviceInfo.getPort(), JsonTranslationUtil.toJsonString(remoteInvokeObject), connectionMillis, resultMillis);
36 | }
37 |
38 | private static Object getInvoke(String remoteActorSystemName, String remoteHost, String port, Object msg, long connectionMillis, long resultMillis){
39 | try {
40 | if (connectionMillis > 0 && resultMillis > 0){
41 | return Await.result(getInvokeFuture(remoteActorSystemName, remoteHost, port, msg, new Timeout(Duration.create(connectionMillis, "millis"))), new Timeout(Duration.create(resultMillis, "millis")).duration());
42 | } else {
43 | return Await.result(getInvokeFuture(remoteActorSystemName, remoteHost, port, msg, REQUEST_TIMEOUT), REQUEST_TIMEOUT.duration());
44 | }
45 | } catch (Exception e) {
46 | LogUtils.error(e.getMessage(), e);
47 | return null;
48 | }
49 | }
50 |
51 | private static Future