Tag: 异步

如何在Android上建立异步URL连接?

我使用以下类连接到我的Web服务。 我想让这个异步。 我怎样才能做到这一点? package org.stocktwits.helper; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class RestClient{ private static String convertStreamToString(InputStream is) { /* * To convert the InputStream to String we use the BufferedReader.readLine() * method. We […]

在最初接收的线程之外访问HttpSession

我正在使用Spring 3.当控制器获得请求时,它将控制传递给在Service bean中使用@Async注释的方法someMethod() ,然后返回。 当我访问someMethod() HttpSession对象时,我收到此exception java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: […]

非法监控状态exception

如何将轮询线程传递给另一个线程进行处理。 程序执行在一个控制器类中,它有一个main方法和一个线程池: 主类控制器 public static void main(String[] args) throws InterruptedException { RunnableController controller = new RunnableController(); System.out.println(incomingQueue.size()); controller.initializeDb(); controller.initialiseThreads(); System.out.println(“Polling”); controller.initialUpdate(); } 具有轮询类的线程的方法 private void initialiseThreads() { try { threadExecutorRead = Executors.newFixedThreadPool(10); PollingSynchronizer reader = new PollingSynchronizer(incomingQueue,dbConnection); threadExecutorRead.submit(reader); }catch (Exception e){ e.printStackTrace(); } } 具有proccesor类的线程的方法 private void initialUpdate() { RunnableController.outgoingQueue = incomingQueue; if((RunnableController.outgoingQueue)!= null){ try […]

如何在继续之前等待异步http结束?

在GWT中有没有办法等待异步调用完成? 我需要继续响应(这是一个登录屏幕,所以成功意味着改变到实际游戏,失败意味着留在登录屏幕)。 这是电话: private void execRequest(RequestBuilder builder) { try { builder.sendRequest(null, new RequestCallback() { public void onError(Request request, Throwable exception) { s = exception.getMessage(); } public void onResponseReceived(Request request, Response response) { s = response.getText(); } }); } catch (RequestException e) { s = e.getMessage(); } } 这是调用方法:` public String check() { s = null; […]

Tomcat 7异步处理失败 – 只有一个请求同时处理

我试图使用Servlet API 3中定义的异步处理实现COMET聊天。它无法正常工作 – 聊天被阻止,所以我创建了调试servlet来仅测试异步部分。 这是我的doGet方法: @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.debug(“doGet called”); int timeout = 30 + RandomUtils.nextInt(60); String message = RandomStringUtils.randomAlphanumeric(50 + RandomUtils.nextInt(250)); response.setHeader(“Access-Control-Allow-Origin”, “*”); final AsyncContext context = request.startAsync(); synchronized(items) { items.add(new RequestItem(context, message, timeout)); } log.debug(“doGet created request and finished”); } 我正在将请求项放入队列中,并且有一个线程正在运行,它将在指定的超时后获取项目并向AsyncContext发送响应,打印有关它的消息。 问题是,线程被阻塞,直到AsyncContext得到响应。 这是在浏览器中请求4页加载后在我的日志中可见的内容: 2011-12-08 […]

JavaEE应用服务器中的CompletableFuture / parallelStream

鉴于新的Java8,我们为异步任务获得了非常好的function,例如CompletableFuture和.paralellStream()。 如果你在Java SE中运行它,因为我已经理解它你将使用ForkJoinPool,但是如果我在例如Wildfly或TomcatEE中运行以下示例会发生什么? //Here I start a comp.Future without giving an Executor test = CompletableFuture.supplyAsync(() -> timeConsumingMethod()); //Here I start a parallel stream mList.paralell().filter(…).collect(Collectors.toList()) 会发生什么,我将从哪里借用我的资源 这些示例在@Stateful bean中运行 这些示例在@Stateless bean中运行 这些示例在CDI bean中运行

Spring XML相当于@EnableAsync

有没有办法从XML打开Spring的Async配置? 我看到的所有示例都使用程序化上下文声明并使用@EnableAsync 是否有XML等价物。 在某些地方我看到了正在使用,但是这并未提及有关异步的任何内容。 我正在使用Spring 4。

如何使用CompletionHandlers和小于请求的ByteBuffer读取请求?

我使用的是Java 7和AsynchronousSocketChannel 。 我想阅读一个请求(例如HTTP POST )但是我很难想出一个很好的解决方案来读取完整的请求,如果它大于我正在使用的ByteBuffer的大小。 例如,如果ByteBuffer是4048字节,并且HTTP POST包含大于4kB的图像。 这有什么好的递归解决方案或循环吗? 这是我的阅读请求代码: public void readRequest(final AsynchronousSocketChannel ch) { final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); final StringBuilder strBuilder = new StringBuilder(); final CharsetDecoder decoder = Charset.forName(“US-ASCII”).newDecoder(); ch.read(buffer, null, new CompletionHandler() { public void completed(Integer bytes, Void att) { buffer.flip(); try { decoder.reset(); strBuilder.append(decoder.decode(buffer).toString()); } catch (CharacterCodingException e) { e.printStackTrace(); […]

CompletableFuture检查exception

使用Java 8强大functionCompletableFuture,我想使用此新function的exception转换旧的异步代码。 但是经过检查的例外情况令我困扰。 这是我的代码。 CompletableFuture asyncTaskCompletableFuture = CompletableFuture.supplyAsync(t -> processor.process(taskParam)); process方法的签名: public void process(Message msg) throws MyException; 如何处理ComletableFuture中的已检查exception?

异步日志记录

现在在我的应用程序中,在某些时候我们将一些重要的东西记录到日志文件中。 基本上只针对日志记录,我们正在创建可用数据的JSON,然后登录到日志文件。这是以JSON格式记录数据的业务要求。 现在,从可用数据创建JSON,然后记录到FILE会花费大量时间并影响原始请求返回时间。 现在的想法是改善引用。 我们讨论过的一件事是使用创建线程池 Executors.newSingleThreadExecutor() 在我们的代码中,然后将任务提交给它,将数据转换为JSON和后续日志记录。 这是一个很好的方法吗?因为我们正在管理线程池本身,是否会产生一些问题? 如果有人可以分享更好的解决方案,我将不胜 在某种程度上使用Log4j。我试图使用AsyncAppender但没有达到任何预期的结果。 我们使用的是EJB 3,Jboss 5.0,Log4j,java6。