Skip to content

Commit ae388ec

Browse files
authored
Merge pull request #53 from yaolinxue/develop
注解FlowerService添加方法type(),用来标注服务是否为聚合服务
2 parents e2658a7 + 6dc08ba commit ae388ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+812
-258
lines changed

flower.common/src/main/java/com/ly/train/flower/common/actor/ServiceActor.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
import com.ly.train.flower.common.service.Complete;
2828
import com.ly.train.flower.common.service.FlowerService;
2929
import com.ly.train.flower.common.service.Service;
30-
import com.ly.train.flower.common.service.ServiceConfig;
31-
import com.ly.train.flower.common.service.ServiceFlow;
30+
import com.ly.train.flower.common.service.config.ServiceConfig;
3231
import com.ly.train.flower.common.service.container.ServiceContext;
3332
import com.ly.train.flower.common.service.container.ServiceFactory;
33+
import com.ly.train.flower.common.service.container.ServiceFlow;
3434
import com.ly.train.flower.common.service.container.ServiceLoader;
35+
import com.ly.train.flower.common.service.impl.AggregateService;
3536
import com.ly.train.flower.common.service.message.Condition;
3637
import com.ly.train.flower.common.service.message.FlowMessage;
3738
import com.ly.train.flower.common.service.web.Flush;
3839
import com.ly.train.flower.common.service.web.HttpComplete;
3940
import com.ly.train.flower.common.service.web.Web;
4041
import com.ly.train.flower.common.util.CloneUtil;
41-
import com.ly.train.flower.common.util.Constant;
4242
import akka.actor.AbstractActor;
4343
import akka.actor.ActorRef;
4444
import akka.actor.ActorSystem;
@@ -82,7 +82,7 @@ public ServiceActor(String flowName, String serviceName, int index, ActorSystem
8282
for (ServiceConfig serviceConfig : serviceConfigs) {
8383
RefType refType = new RefType();
8484

85-
if (ServiceFactory.getServiceClassName(serviceConfig.getServiceName()).equals(Constant.AGGREGATE_SERVICE_NAME)) {
85+
if (serviceConfig.isAggregateService()) {
8686
refType.setJoint(true);
8787
}
8888
refType.setActorRef(ServiceActorFactory.buildServiceActor(flowName, serviceConfig.getServiceName(), index));
@@ -152,7 +152,7 @@ public void onReceive(ServiceContext serviceContext) throws Throwable {
152152
if (result == null) {// for joint service
153153
return;
154154
}
155-
155+
156156
for (RefType refType : nextServiceActors) {
157157
Object resultClone = CloneUtil.clone(result);
158158
ServiceContext context = serviceContext.newInstance();
@@ -178,7 +178,8 @@ public FlowerService getService() {
178178
if (this.service == null) {
179179
this.service = ServiceFactory.getService(serviceName);
180180
if (service instanceof Aggregate) {
181-
((Aggregate) service).setSourceNumber(ServiceFlow.getOrCreate(flowName).getServiceConfig(serviceName).getJointSourceNumber());
181+
((AggregateService) service)
182+
.setSourceNumber(ServiceFlow.getOrCreate(flowName).getServiceConfig(serviceName).getJointSourceNumber());
182183
}
183184
}
184185
return service;

flower.common/src/main/java/com/ly/train/flower/common/actor/ServiceFacade.java

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.concurrent.ConcurrentHashMap;
2121
import java.util.concurrent.TimeUnit;
2222
import javax.servlet.AsyncContext;
23-
import com.ly.train.flower.common.service.ServiceFlow;
2423
import com.ly.train.flower.common.service.container.ServiceContext;
24+
import com.ly.train.flower.common.service.container.ServiceFlow;
2525
import com.ly.train.flower.logging.Logger;
2626
import com.ly.train.flower.logging.LoggerFactory;
2727
import akka.actor.ActorRef;
@@ -38,42 +38,78 @@ public class ServiceFacade {
3838
// TODO user define duration
3939
static FiniteDuration duration = Duration.create(3, TimeUnit.SECONDS);
4040

41+
/**
42+
* @deprecated serviceName 不必须,因为可以从流程中获取到首个服务
43+
*/
44+
@Deprecated
4145
public static void asyncCallService(String flowName, String serviceName, Object message, AsyncContext ctx) throws IOException {
42-
ServiceContext context = ServiceContext.context(message, ctx);
46+
asyncCallService(flowName, message, ctx);
47+
}
48+
49+
/**
50+
* 异步处理服务
51+
*
52+
* @param flowName 流程名称
53+
* @param message 消息体
54+
* @param asyncContext 异步处理上下文
55+
* @throws IOException io exception
56+
*/
57+
public static void asyncCallService(String flowName, Object message, AsyncContext asyncContext) throws IOException {
58+
ServiceContext context = ServiceContext.context(message, asyncContext);
4359
context.setFlowName(flowName);
44-
serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
60+
String serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
4561
ServiceActorFactory.buildServiceActor(flowName, serviceName).tell(context, ActorRef.noSender());
4662
}
4763

64+
/**
65+
*
66+
* @see asyncCallService
67+
*/
68+
@Deprecated
4869
public static void asyncCallService(String flowName, String serviceName, Object message) throws IOException {
49-
asyncCallService(flowName, serviceName, message, null);
70+
asyncCallService(flowName, message, null);
71+
}
72+
73+
/**
74+
* @see asyncCallService
75+
*/
76+
public static void asyncCallService(String flowName, Object message) throws IOException {
77+
asyncCallService(flowName, message, null);
5078
}
5179

5280

5381
/**
5482
* 同步调用会引起阻塞,因此需要在外面try catch异常TimeoutException
5583
*
56-
* @param flowName
57-
* @param serviceName
58-
* @param message
59-
* @return
84+
* @param flowName flowName
85+
* @param serviceName serviceName
86+
* @param message message
87+
* @return object
6088
* @throws Exception
6189
*/
62-
public static Object syncCallService(String flowName, String serviceName, Object message) throws Exception {
90+
public static Object syncCallService(String flowName, Object message) throws Exception {
6391
ServiceContext context = ServiceContext.context(message);
6492
context.setSync(true);
65-
serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
93+
String serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
6694
return Await.result(Patterns.ask(ServiceActorFactory.buildServiceActor(flowName, serviceName), context, new Timeout(duration)),
6795
duration);
6896
}
6997

98+
/**
99+
* @deprecated serviceName 不必须,因为可以从流程中获取到首个服务
100+
*/
101+
@Deprecated
102+
public static Object syncCallService(String flowName, String serviceName, Object message) throws Exception {
103+
return syncCallService(flowName, message);
104+
}
105+
70106
/**
71107
* will cache by flowName + "_" + serviceName
72108
*
73-
* @param flowName
74-
* @param serviceName
109+
* @param flowName flowName
110+
* @param serviceName serviceName
75111
* @param flowNumbe 数量
76-
* @return
112+
* @return {@link ServiceRouter}
77113
*/
78114
public static ServiceRouter buildServiceRouter(String flowName, String serviceName, int flowNumbe) {
79115
serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();

flower.common/src/main/java/com/ly/train/flower/common/actor/ServiceRouter.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public <T> void asyncCallService(T message, AsyncContext ctx) throws IOException
6262
* 同步调用
6363
*
6464
* @param message message
65-
* @return
65+
* @return obj
6666
* @throws Exception
6767
*/
6868
public Object syncCallService(Object message) throws Exception {
@@ -94,7 +94,7 @@ protected synchronized int roundIndex() {
9494
/**
9595
* 当actor个数为2^n个数时才可以使用
9696
*
97-
* @return
97+
* @return int
9898
*/
9999
protected int bitRandomIndex() {
100100
if (number == 1) {
@@ -106,9 +106,6 @@ protected int bitRandomIndex() {
106106
return (currentIndex++) & (number - 1);
107107
}
108108

109-
/**
110-
* @return
111-
*/
112109
protected int moduleRandomIndex() {
113110
if (number == 1) {
114111
return 0;

flower.common/src/main/java/com/ly/train/flower/common/annotation/FlowerService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@
4040
*/
4141
String value() default "";
4242

43+
44+
/**
45+
* 类型,默认是普通类型
46+
*
47+
* @return {@link FlowerType}
48+
*/
49+
FlowerType type() default FlowerType.COMMON;
50+
4351
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright © 2019 同程艺龙 (zhihui.li@ly.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/**
17+
*
18+
*/
19+
package com.ly.train.flower.common.annotation;
20+
21+
/**
22+
* @author leeyazhou
23+
*
24+
*/
25+
public enum FlowerType {
26+
27+
/**
28+
* 普通类型
29+
*/
30+
COMMON,
31+
32+
/**
33+
* 聚合类型
34+
*/
35+
AGGREGATE
36+
37+
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright © 2019 同程艺龙 (zhihui.li@ly.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/**
17+
*
18+
*/
19+
package com.ly.train.flower.common.exception;
20+
21+
import com.ly.train.flower.logging.Logger;
22+
import com.ly.train.flower.logging.LoggerFactory;
23+
24+
/**
25+
* @author leeyazhou
26+
*
27+
*/
28+
public class ExceptionHandler {
29+
protected static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);
30+
31+
public static void handle(Throwable throwable) {
32+
logger.error("", throwable);
33+
}
34+
}

flower.common/src/main/java/com/ly/train/flower/common/service/Aggregate.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@
1717

1818
public interface Aggregate {
1919

20-
void setSourceNumber(int number);
2120

2221
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright © 2019 同程艺龙 (zhihui.li@ly.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/**
17+
*
18+
*/
19+
package com.ly.train.flower.common.service.config;
20+
21+
import java.io.Serializable;
22+
23+
/**
24+
* @author leeyazhou
25+
*
26+
*/
27+
public class FlowConfig implements Serializable {
28+
private static final long serialVersionUID = 1L;
29+
private boolean aggregate;
30+
31+
/**
32+
* @return the aggregate
33+
*/
34+
public boolean isAggregate() {
35+
return aggregate;
36+
}
37+
}

flower.common/src/main/java/com/ly/train/flower/common/service/ServiceConfig.java renamed to flower.common/src/main/java/com/ly/train/flower/common/service/config/ServiceConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.ly.train.flower.common.service;
16+
package com.ly.train.flower.common.service.config;
1717

1818
import java.io.Serializable;
1919
import java.util.HashSet;
2020
import java.util.Set;
2121
import java.util.concurrent.atomic.AtomicInteger;
22+
import com.ly.train.flower.common.service.container.ServiceFactory;
23+
import com.ly.train.flower.common.util.Constant;
2224

2325
/**
2426
* 流程服务节点配置
@@ -99,6 +101,15 @@ public boolean hasPreviousServices() {
99101
return previousServiceConfigs != null && previousServiceConfigs.size() > 0;
100102
}
101103

104+
/**
105+
* 聚合服务
106+
*
107+
* @return true / false
108+
*/
109+
public boolean isAggregateService() {
110+
return ServiceFactory.getServiceClassName(getServiceName()).equals(Constant.AGGREGATE_SERVICE_NAME);
111+
}
112+
102113
public String getSimpleDesc() {
103114
StringBuilder sb = new StringBuilder();
104115
sb.append(getServiceName());

flower.common/src/main/java/com/ly/train/flower/common/service/container/ServiceContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public static <T> ServiceContext context(T message, AsyncContext ctx) {
5858
/**
5959
* 从当前对象创建一个副本
6060
*
61-
* @param context
62-
* @return
61+
* @return {@link ServiceContext}
6362
*/
6463
public ServiceContext newInstance() {
6564
ServiceContext serviceContext = new ServiceContext();

0 commit comments

Comments
 (0)