【发布时间】:2015-06-30 11:33:07
【问题描述】:
我正在开发一个项目,该项目有两种风格,有和没有多租户。
该项目公开了一个我希望异步的 REST 服务。 所以我的基本服务看起来像
@Component
@Path("/resouce")
@Consumes(MediaType.APPLICATION_JSON)
public class ResouceEndpoint {
@POST
@ManagedAsync
public void add(final Event event, @Suspended final AsyncResponse asyncResponse) {
resouce.insert (event);
asyncResponse.resume( Response.status(Response.Status.NO_CONTENT).build());
}
}
没有多租户也可以正常工作,而且我可以免费获得内部 Jersey executor 服务的好处。见@ManagedAsync
当我切换到多租户时,我会在请求中添加一个过滤器来解析租户 ID,并将其放置在本地线程(在我们的例子中是 HTTP 线程)上。
当处理链点击“add()”方法时,当前线程上面是 Jersey executor 服务提供的,所以不包含我的租户 id。 我只能考虑以下选项来解决此问题。
将 ResouceEndpoint 扩展到 MutliTenantResouceEndpoint 并删除 @ManagedAsync 使用我自己的线程执行器
public class MutliTenantResouceEndpoint extends ResouceEndpoint {
@POST
public void add(final Event event, @Suspended final AsyncResponse asyncResponse) {
final String tenantId = getTeantIdFromThreadLocal();
taskExecutor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
setTeantIdToThreadLocal(tenantId);
browserEventsAnalyzer.insertEvent(event);
Response response = Response.status(Response.Status.NO_CONTENT).build();
asyncResponse.resume(response);
return null;
}
});
}
}
但是这样我需要管理我自己的线程执行器,感觉就像我在这里遗漏了一些东西。 对不同的方法有什么建议吗?
【问题讨论】:
标签: java multithreading rest jersey servlet-3.0