使用注释将 BayeuxService 注入另一个类时出现问题
我有一个网络应用程序,它使用 Bayeux 来处理 Comet 连接。我初始化了一个 BayeuxServer 并将其绑定到 Spring 注释中,一切正常,监听选定的通道并做出响应。
我有一个 Jersey 带注释的类和一个带注释的 Bayeux 服务,如下所示。我的想法是,我希望能够通过单个 Web 应用程序中的 Rest 控制资源,然后在资源更改后,通过 Comet 向所有其他适用的客户端进行服务器推送,告诉他们更新信息。
问题是:部署 Web 应用程序时会创建 Bayeux 服务,设置适当的通道来侦听和监视客户端。这种情况应该只有一个例子。当 Jersey 尝试使用 Bayeux 服务时,它会创建一个全新的服务,而它应该使用原始服务。这个新服务没有正确注入 BayeuxServer,因此我无法通过它访问客户端信息。
因为这应该是可行的,但我似乎不明白如何通过注释正确注入这些东西。有人能指出我正确的方向吗?
泽西岛注释类:
@Path("JsonTest")
public class JsonTest {
@Context
Request request;
@Context
UriInfo uriInfo;
@Context
ResourceContext resourceContext;
protected final Logger log = Logger.getLogger(getClass());
public JsonTest() {
}
@DELETE
@Path("{id}")
public void deleteJson(@PathParam("id") String id) {
JsonTestDao.instance.getModel().remove(id);
log.info("Deleted Json..." + id);
log.info("New json: " + JsonTestDao.instance.getModel().toString());
JsonTestService jsonTestService = resourceContext.getResource(JsonTestService.class);
jsonTestService.sendUpdate();
}
}
BayeuxService:
@Named
// Singleton here didn't seem to make a difference
@Service
public class JsonTestService {
protected final Logger log = Logger.getLogger(getClass());
@Inject
private BayeuxServer bayeux;
@Session
private ServerSession serverSession;
@PostConstruct
public void init() {
log.info("Initializing JsonTest Bayeux HelloService...");
log.info("Current sessions are: " + bayeux.getSessions().toString());
}
@Listener("/cometd/JsonTest")
public void jsonTestHandler(ServerSession remote, ServerMessage.Mutable message) {
}
public void sendUpdate() {
//bayeux.newMessage(); // Need a method that the Jersey class can call to notify changes
log.info("Bayeux server should be sending an update now...");
}
@PreDestroy
public void destroy() {
log.info("Destroying JsonTest Bayeux HelloService...");
}
}
I have a web app that is using Bayeux to handle Comet connections. I initialize a BayeuxServer and tie it into Spring annotations and it all works fine, listening on selected channels and responding.
I have a Jersey annotated class and an annotated Bayeux service as shown below. The idea is I wanted to be able to control resources via Rest from an individual web app, and then right after the resource is changed, do a server push via Comet to all other applicable clients to tell them to update their information.
Here is the problem: A Bayeux Service is created when the webapp is deployed, setting up proper channels to listen on and monitoring clients. There should only be one instance of this. When Jersey attempts to use the Bayeux service it creates a whole new service, when it should be using the original one. This new service doesn't have the BayeuxServer properly injected so I can't access client information through it.
It makes since that this should be doable, but I don't seem to understand how to inject these things properly via annotations. Can anyone point me in the right direction?
Jersey Annotated Class:
@Path("JsonTest")
public class JsonTest {
@Context
Request request;
@Context
UriInfo uriInfo;
@Context
ResourceContext resourceContext;
protected final Logger log = Logger.getLogger(getClass());
public JsonTest() {
}
@DELETE
@Path("{id}")
public void deleteJson(@PathParam("id") String id) {
JsonTestDao.instance.getModel().remove(id);
log.info("Deleted Json..." + id);
log.info("New json: " + JsonTestDao.instance.getModel().toString());
JsonTestService jsonTestService = resourceContext.getResource(JsonTestService.class);
jsonTestService.sendUpdate();
}
}
BayeuxService:
@Named
// Singleton here didn't seem to make a difference
@Service
public class JsonTestService {
protected final Logger log = Logger.getLogger(getClass());
@Inject
private BayeuxServer bayeux;
@Session
private ServerSession serverSession;
@PostConstruct
public void init() {
log.info("Initializing JsonTest Bayeux HelloService...");
log.info("Current sessions are: " + bayeux.getSessions().toString());
}
@Listener("/cometd/JsonTest")
public void jsonTestHandler(ServerSession remote, ServerMessage.Mutable message) {
}
public void sendUpdate() {
//bayeux.newMessage(); // Need a method that the Jersey class can call to notify changes
log.info("Bayeux server should be sending an update now...");
}
@PreDestroy
public void destroy() {
log.info("Destroying JsonTest Bayeux HelloService...");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 Jersey 和 spring 集成 - bean 注入在运行时为 null 。
我问的另一个问题。这两个问题都源于同样的问题,涉及正确设置 Jersey 依赖并将其与 spring 集成。
See Jersey and spring integration - bean Injections are null at runtime.
Another question I asked. Both of these stem from the same problem involving properly setting the Jersey dependency and integrating it with spring.