[英]Manage long-running connection to Twitter streaming API from java EE application
I need to create a long-running connection from within a Java EE6 application to the Twitter streaming API and also be able to monitor and re-establish the connection on failure. 我需要创建一个从Java EE6应用程序到Twitter流API的长时间运行的连接,还需要监视并在失败时重新建立连接。
I've run up a test with Jersey JAX-RS and OAuth as follows and this handles the streaming data as I would like. 我已经对Jersey JAX-RS和OAuth进行了如下测试,它可以根据需要处理流数据。
WebResource resource = client.resource(STATUS_URI);
resource.addFilter(oauthFilter);
ClientResponse clientResponse = resource.get(ClientResponse.class);
InputStream is = clientResponse.getEntityInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF8"));
boolean more = true;
while (more) {
String jsonTweet = r.readLine();
//send tweet to JMS queue
.....
}
I've tried running the above code in a singleton '@PostConstruct' method but it blocks and the application does not load properly. 我尝试以单例“ @PostConstruct”方法运行以上代码,但该代码会阻塞,并且应用程序无法正确加载。
My question is what would be the recommended way to initialise the connection from within a Java EE environment on application startup and then monitor + re-connect on errors without directly creating new threads? 我的问题是在应用程序启动时从Java EE环境中初始化连接,然后监视并重新连接错误而不直接创建新线程的推荐方法是什么?
recommended way to initialise the connection from within a Java EE environment on application startup
在应用程序启动时从Java EE环境中初始化连接的推荐方法
@Service
to create Singleton MBean(JBoss Specific implementation), but still I prefer @Startup
& @Singleton
to get things done at application deployment for portability with server. @Service
创建Singleton MBean(JBoss特定实现),但是我还是更喜欢@Startup
和@Singleton
在应用程序部署中完成工作,以实现服务器的可移植性。 monitor + re-connect on errors without directly creating new threads?
监视+重新连接错误而不直接创建新线程?
In exception, tasks can be resubmitted to executor service, task may comprise of monitoring, reconnecting the connection etc. 在例外情况下,可以将任务重新提交给执行者服务,任务可以包括监视,重新连接连接等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.