8000 注解FlowerService添加方法type(),用来标注服务是否为聚合服务 by leeyazhou · Pull Request #53 · zhihuili/flower · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

注解FlowerService添加方法type(),用来标注服务是否为聚合服务 #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions flower.common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-slf4j_2.12</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.12</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
import com.ly.train.flower.common.service.Complete;
import com.ly.train.flower.common.service.FlowerService;
import com.ly.train.flower.common.service.Service;
import com.ly.train.flower.common.service.ServiceConfig;
import com.ly.train.flower.common.service.ServiceFlow;
import com.ly.train.flower.common.service.config.ServiceConfig;
import com.ly.train.flower.common.service.container.ServiceContext;
import com.ly.train.flower.common.service.container.ServiceFactory;
import com.ly.train.flower.common.service.container.ServiceFlow;
import com.ly.train.flower.common.service.container.ServiceLoader;
import com.ly.train.flower.common.service.impl.AggregateService;
import com.ly.train.flower.common.service.message.Condition;
import com.ly.train.flower.common.service.message.FlowMessage;
import com.ly.train.flower.common.service.web.Flush;
import com.ly.train.flower.common.service.web.HttpComplete;
import com.ly.train.flower.common.service.web.Web;
import com.ly.train.flower.common.util.CloneUtil;
import com.ly.train.flower.common.util.Constant;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
Expand Down Expand Up @@ -82,7 +82,7 @@ public ServiceActor(String flowName, String serviceName, int index, ActorSystem
for (ServiceConfig serviceConfig : serviceConfigs) {
RefType refType = new RefType();

if (ServiceFactory.getServiceClassName(serviceConfig.getServiceName()).equals(Constant.AGGREGATE_SERVICE_NAME)) {
if (serviceConfig.isAggregateService()) {
refType.setJoint(true);
}
refType.setActorRef(ServiceActorFactory.buildServiceActor(flowName, serviceConfig.getServiceName(), index));
Expand Down Expand Up @@ -152,7 +152,7 @@ public void onReceive(ServiceContext serviceContext) throws Throwable {
if (result == null) {// for joint service
return;
}

for (RefType refType : nextServiceActors) {
Object resultClone = CloneUtil.clone(result);
ServiceContext context = serviceContext.newInstance();
Expand All @@ -178,7 +178,8 @@ public FlowerService getService() {
if (this.service == null) {
this.service = ServiceFactory.getService(serviceName);
if (service instanceof Aggregate) {
((Aggregate) service).setSourceNumber(ServiceFlow.getOrCreate(flowName).getServiceConfig(serviceName).getJointSourceNumber());
((AggregateService) service)
.setSourceNumber(ServiceFlow.getOrCreate(flowName).getServiceConfig(serviceName).getJointSourceNumber());
}
}
return service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import javax.servlet.AsyncContext;
import com.ly.train.flower.common.service.ServiceFlow;
import com.ly.train.flower.common.service.container.ServiceContext;
import com.ly.train.flower.common.service.container.ServiceFlow;
import com.ly.train.flower.logging.Logger;
import com.ly.train.flower.logging.LoggerFactory;
import akka.actor.ActorRef;
Expand All @@ -38,42 +38,78 @@ public class ServiceFacade {
// TODO user define duration
static FiniteDuration duration = Duration.create(3, TimeUnit.SECONDS);

/**
* @deprecated serviceName 不必须,因为可以从流程中获取到首个服务
*/
@Deprecated
public static void asyncCallService(String flowName, String serviceName, Object message, AsyncContext ctx) throws IOException {
ServiceContext context = ServiceContext.context(message, ctx);
asyncCallService(flowName, message, ctx);
}

/**
* 异步处理服务
*
* @param flowName 流程名称
* @param message 消息体
* @param asyncContext 异步处理上下文
* @throws IOException io exception
*/
public static void asyncCallService(String flowName, Object message, AsyncContext asyncContext) throws IOException {
ServiceContext context = ServiceContext.context(message, asyncContext);
context.setFlowName(flowName);
serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
String serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
ServiceActorFactory.buildServiceActor(flowName, serviceName).tell(context, ActorRef.noSender());
}

/**
*
* @see asyncCallService
*/
@Deprecated
public static void asyncCallService(String flowName, String serviceName, Object message) throws IOException {
asyncCallService(flowName, serviceName, message, null);
asyncCallService(flowName, message, null);
}

/**
* @see asyncCallService
*/
public static void asyncCallService(String flowName, Object message) throws IOException {
asyncCallService(flowName, message, null);
}


/**
* 同步调用会引起阻塞,因此需要在外面try catch异常TimeoutException
*
* @param flowName
* @param serviceName
* @param message
* @return
* @param flowName flowName
* @param serviceName serviceName
* @param message message
* @return object
* @throws Exception
*/
public static Object syncCallService(String flowName, String serviceName, Object message) throws Exception {
public static Object syncCallService(String flowName, Object message) throws Exception {
ServiceContext context = ServiceContext.context(message);
context.setSync(true);
serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
String serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
return Await.result(Patterns.ask(ServiceActorFactory.buildServiceActor(flowName, serviceName), context, new Timeout(duration)),
duration);
}

/**
* @deprecated serviceName 不必须,因为可以从流程中获取到首个服务
*/
@Deprecated
public static Object syncCallService(String flowName, String serviceName, Object message) throws Exception {
return syncCallService(flowName, message);
}

/**
* will cache by flowName + "_" + serviceName
*
* @param flowName
* @param serviceName
* @param flowName flowName
* @param serviceName serviceName
* @param flowNumbe 数量
* @return
* @return {@link ServiceRouter}
*/
public static ServiceRouter buildServiceRouter(String flowName, String serviceName, int flowNumbe) {
serviceName = ServiceFlow.getOrCreate(flowName).getHeadServiceConfig().getServiceName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public <T> void asyncCallService(T message, AsyncContext ctx) throws IOException
* 同步调用
*
* @param message message
* @return
* @return obj
* @throws Exception
*/
public Object syncCallService(Object message) throws Exception {
Expand Down Expand Up @@ -94,7 +94,7 @@ protected synchronized int roundIndex() {
/**
* 当actor个数为2^n个数时才可以使用
*
* @return
* @return int
*/
protected int bitRandomIndex() {
if (number == 1) {
Expand All @@ -106,9 +106,6 @@ protected int bitRandomIndex() {
return (currentIndex++) & (number - 1);
}

/**
* @return
*/
protected int moduleRandomIndex() {
if (number == 1) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@
*/
String value() default "";


/**
* 类型,默认是普通类型
*
* @return {@link FlowerType}
*/
FlowerType type() default FlowerType.COMMON;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright © 2019 同程艺龙 (zhihui.li@ly.com)
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.ly.train.flower.common.annotation;

/**
* @author leeyazhou
*
*/
public enum FlowerType {

/**
* 普通类型
*/
COMMON,

/**
* 聚合类型
*/
AGGREGATE


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright © 2019 同程艺龙 (zhihui.li@ly.com)
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.ly.train.flower.common.exception;

import com.ly.train.flower.logging.Logger;
import com.ly.train.flower.logging.LoggerFactory;

/**
* @author leeyazhou
*
*/
public class ExceptionHandler {
protected static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

public static void handle(Throwable throwable) {
logger.error("", throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@

public interface Aggregate {

void setSourceNumber(int number);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright © 2019 同程艺龙 (zhihui.li@ly.com)
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.ly.train.flower.common.service.config;

import java.io.Serializable;

/**
* @author leeyazhou
*
*/
public class FlowConfig implements Serializable {
private static final long serialVersionUID = 1L;
private boolean aggregate;

/**
* @return the aggregate
*/
public boolean isAggregate() {
return aggregate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ly.train.flower.common.service;
package com.ly.train.flower.common.service.config;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import com.ly.train.flower.common.service.container.ServiceFactory;
import com.ly.train.flower.common.util.Constant;

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

/**
* 聚合服务
*
* @return true / false
*/
public boolean isAggregateService() {
return ServiceFactory.getServiceClassName(getServiceName()).equals(Constant.AGGREGATE_SERVICE_NAME);
}

public String getSimpleDesc() {
StringBuilder sb = new StringBuilder();
sb.append(getServiceName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public static <T> ServiceContext context(T message, AsyncContext ctx) {
/**
* 从当前对象创建一个副本
*
* @param context
* @return
* @return {@link ServiceContext}
*/
public ServiceContext newInstance() {
ServiceContext serviceContext = new ServiceContext();
Expand Down
Loading
0