简体   繁体   English

从Java EE应用程序管理与Twitter流API的长期连接

[英]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环境中初始化连接的推荐方法

  • You can use @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? 监视+重新连接错误而不直接创建新线程?

  • ThreadPoolExecutor : An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods. ThreadPoolExecutor:一个ExecutorService,它使用可能是多个池线程中的一个来执行每个提交的任务,通常使用Executors工厂方法进行配置。

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.

相关问题 来自Java EE Application Server的数据流 - Data Streaming from Java EE Application Server 将Twitter Streaming API集成到Java Web应用程序中 - Integrating twitter streaming API into a java web application 创建、启动和管理长时间运行的线程的最简洁的方法是什么? - What is the cleanest way to create, start, and manage long-running threads? 如何在 Dropwizard/Jersey 中保持长时间运行的 HTTP 连接? - How to keep a long-running HTTP connection alive in Dropwizard/Jersey? Java中长期运行的数据处理系统的通用架构? - General architecture for a long-running data-processing system in Java? Java的轻量级长时间运行方法取消模式 - Lightweight long-running method cancel pattern for Java 哪种Java容器可用于EC2上长时间运行的进程? - What sort of Java container for long-running processes on EC2? 终止长时间运行的进程_and_接收Java中的stdout - Terminate long-running process _and_ receive stdout in Java 从Apache ServiceMix内部启动长时间运行的过程 - Starting long-running process from within Apache ServiceMix 如何从 EDT 将 object 交付给长期运行的 SwingWorker? - How to deliver object to a long-running SwingWorker from the EDT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM