Category Archives: asynchronous

Apache Axis2: Asynchronous WebService leads to numerous non-terminating Axis2 Task Threads

We wanted to experiment with nonblocking API provided by Apache Axis2.

We found that there is a background thread which is named as 'Axis2 Task', which is created.

Even after the task is completed, the thread is still not getting terminated and is present in waiting state.

Due to this, we are seeing the scenario of lot of Axis2 Task threads being present in parked state and the CPU is being hogged.

What has been tried?

  • ServiceClient.cleanup() is invoked in finally block, still the thread is present

Related issues:

How to change response’s status code from HttpAsyncContentProducer or defer response submission until content is ready

I have a class MyHttpAsyncHandler which implements HttpAsyncRequestHandler<HttpRequest>.
Once a request is received, it is handled via the handle(HttpRequest, HttpAsyncExchange, HttpContext) method, which I have overridden:

    HttpResponse response = httpExchange.getResponse();
    response.setHeader("Content-Type", String.valueOf(ContentType.APPLICATION_JSON));
    handleInternal(request, response, context);
    httpExchange.submitResponse(new BasicAsyncResponseProducer(response)); 

In the handleInternal method, after some processing, I define a BasicHttpEntity and assign to it a class I've written which extends BasicHttpEntity and implements HttpAsyncContentProducer, like this:

BasicHttpEntity entity = new MyResultEntity();
// Setting the status as 200, since this is the default case.
response.setStatusCode(HttpStatus.SC_OK); 
response.setEntity(entity);

Let's say that during the execution of the request, the "back-end" might end-up with an error.
In the MyResultEntity, during produceContent, I can detect that error and I want to reply to the client with status code (500) and supply an informative error.

Question
I have managed to supply an informative error, but I cannot manage to reply with a status code of 500.
Is there any way to do that?

What I have tried so far (and failed)
- Passing the response of handleInternal to the constructor of MyResultEntity, so as to set the status code using the setter. (Also, tried passing HttpExchage, getting response and setting its status to 500 after content is ready.)
- Extending the BasicAsyncResponseProducer (which contains the response object) and during produceContent detecting the error and setting the status code there.

It is also mentioned at the documentation that one can defer submission of response. How can one do this?

Am I missing something? Shouldn't there be a way to change the response's status code via the content producer if something goes wrong?

Additional info
The packages I am using are these:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore-nio</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
    </dependency>

Java / Apache Async Http Client

I would like to do an asynchronous http request in Java. I embedded all the libraries from the HttpAsyncClient 4.1.2 (GA) here: https://hc.apache.org/downloads.cgi.

Here is the code:

        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    try {

        httpclient.start();

        String status = new JSONObject()
                .put("test","test")
                .toString();

        HttpPost request = new HttpPost("my url");
        request.setEntity(new StringEntity(status));

        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        httpclient.execute(request, new FutureCallback<HttpResponse>() {

            public void cancelled() {
                logString("Cancelled");
            }

            public void completed(HttpResponse arg0) {
                logString("Completed");
            }

            public void failed(Exception arg0) {
                logString("Failed");
            }
        });

    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I also embedded the java json library.

Everytime it throws the following exception.

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:53)
at org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:57)
at org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:47)...

Am I missing something?

RouteSpecificPool timeout Occuring processing HTTP request while using NIO

We have a an application in which we are making an asynchronous request to get the Servlet.For making asyncrnous Request we are using apache nio

https://hc.apache.org/httpcomponents-core-ga/tutorial/html/nio.html

I am getting this error.This error is not occuring always and i am not able to figure out when this occurs exactly.

Initially i thought issue was with pool and increased pool size. But some times it occurs if a send single request only although pool size is 1000.

What can be the possible reason for this error shown below:

java.net.ConnectException: null
        at org.apache.http.nio.pool.RouteSpecificPool.timeout(RouteSpecificPool.java:170) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.nio.pool.AbstractNIOConnPool.requestTimeout(AbstractNIOConnPool.java:560) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.nio.pool.AbstractNIOConnPool$InternalSessionRequestCallback.timeout(AbstractNIOConnPool.java:821) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.impl.nio.reactor.SessionRequestImpl.timeout(SessionRequestImpl.java:182) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.

impl.nio.reactor.DefaultConnectingIOReactor.processTimeouts(DefaultConnectingIOReactor.java:212) [httpcore-nio-4.4.4.jar:4.4.4] at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:157) [httpcore-nio-4.4.4.jar:4.4.4] at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:350) [httpcore-nio-4.4.4.jar:4.4.4] at com.sampleApp.test.nio.NotifierReactor$2.run(NotifierReactor.java:93) [classes/:na]

RouteSpecificPool timeout Occuring processing HTTP request while using NIO

We have an application in which we are making an asynchronous request to get the Servlet Response.For making asynchronous Request we are using apache nio

https://hc.apache.org/httpcomponents-core-ga/tutorial/html/nio.html

I am getting this error.This error is not occuring always and i am not able to figure out when this occurs exactly.

Initially i thought issue was with pool and increased pool size. But some times it occurs if a send single request only although pool size is 1000.

What can be the possible reason for this error shown below:

java.net.ConnectException: null
        at org.apache.http.nio.pool.RouteSpecificPool.timeout(RouteSpecificPool.java:170) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.nio.pool.AbstractNIOConnPool.requestTimeout(AbstractNIOConnPool.java:560) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.nio.pool.AbstractNIOConnPool$InternalSessionRequestCallback.timeout(AbstractNIOConnPool.java:821) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.impl.nio.reactor.SessionRequestImpl.timeout(SessionRequestImpl.java:182) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processTimeouts(DefaultConnectingIOReactor.java:212) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:157) [httpcore-nio-4.4.4.jar:4.4.4]
        at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:350) [httpcore-nio-4.4.4.jar:4.4.4]
        at com.sampleApp.test.nio.NotifierReactor$2.run(NotifierReactor.java:93) [classes/:na]

Apache cxf server asynchronously request handling

Hi i need to implement asynchronously handle request in apache cxf server.So far i thought i need to add AsyncResponse to request.But it didn't work.

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
void sample(@Suspended AsyncResponse asyncResponse);

@Override
    public void sample(AsyncResponse asyncResponse) {
        asyncResponse.setTimeout(3, TimeUnit.MINUTES);
        asyncResponse.setTimeoutHandler(new TimeoutHandler() {
            @Override
            public void handleTimeout(AsyncResponse asyncResponse) {
                asyncResponse.resume("Sample");
            }
        });

    }

When i send request to end point it will throw exception.

WebApplicationExceptionMapper - WebApplicationException has been caught, status: 415 
javax.ws.rs.WebApplicationException 
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1054) 
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:614) 
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:578) 
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:238) 
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:89) 
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262) 
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:122) 
    at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.serviceRequest(JettyHTTPDestination.java:344) 
    at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:310) 
    at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:72) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:943) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:879) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110) 
    at org.eclipse.jetty.server.Server.handle(Server.java:345) 
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:441) 
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:919) 
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:582) 
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:218) 
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)

I want to know the way i implemented asynchronously is correct and if it is the way what is the issue with my implementation.

How to get requestCompleted callback on cxf async ws client

I have built an HTTP async client using Apache. In order to receive a notification that a request has been sent, I wrote the following code.

private final CloseableHttpAsyncClient httpclient;

Later

final HttpContext context = ...
final FutureCallback<HttpResponse> callback = ...

final HttpAsyncRequestProducer requestProducer = new BasicAsyncRequestProducer(URIUtils.extractHost(URI.create(serverAddress)), req) {
    @Override
    public void requestCompleted(HttpContext context) {
        // Here I register that the request has been sent, log,  etc.
    }
};
httpclient.execute(requestProducer, HttpAsyncMethods.createConsumer(), context, callback);

This seems to be working fine. Now I'm building Web Services async client using Apache CXF and again, I would like to receive a notification that the message has been sent and I'm not sure how to achieve that.

Now CXF async WS client is said to be using the HTTP async client as a transport layer. That made me think that there might be a way to achieve my goal was by somehow accessing the transport layer API. However, I'm not sure if it is doable or how how to do that.

Or perhaps there might be another way to get that. I suspect that a request handler can only be called before the request is written and therefore it cannot guarantee that an IO error hasn't occurred.

Asynchronous database/service calls in PHP: Gearman vs. pthreads

On our LAMP site, we have a problem with some services having to make several calls to the database to pull data. Usually the way this is done in PHP (at least my experience) is serially; which obviously is inefficient. We can mitigate some of the inefficiencies by using caching and aggregating some queries; but in some cases we need to still need to do multiple db calls.

Ideally, I would just send as many as requests as possible to the db or web services at the same time asynchronously, but PHP doesn't seem to support that pattern out of the box. These are the workarounds I know about to accomplish this.

We currently use Gearman to take care of asynchronous tasks. I could rewrite some our services as Gearman tasks and use that to make async calls to the db and services. However, we've had bad experiences with Gearman eating up a lot of processes and memory; forcing use to restart our production servers on some occasions as they become unresponsive. However, I believe I was able to trace this issue due to some errors in the scripts and believe I have it fixed. But I feel this instability and excessive resource consumption may rear its head again if we use Gearman as a task pool/manager for processing async tasks.

Alternatively, I was looking at pthreads. It seemed like a better alternative as it didn't require having a running Gearman daemon to work, and it accomplished what I wanted. There even seemed to be a decent framework over it, amphp. We decided to configured a test server with pthreads enabled. However, what we didn't know was pthreads required changing our Apache config, specifically going to httpd.worker from httpd(aka prefork). Admittedly, I'm not an experienced Apache admin, so I'm not even sure what the ramifications of making this change will be. Because of this, now I'm hesitant to use it, at least until I have time to research this some more.

So my question is, if I wanted to do asynchronous requests in PHP, should I stick with Gearman knowing I may run into issues, or should I risk it and go with pthreads, even though it seems to require changes to our Apache configuration that I quite frankly don't know how it will affect our site? Or maybe, there is perhaps another option for what I'm trying to do that I just don't know about yet.

Server config:

  • PHP 5.6.1
  • Apache 2.4.12
  • Red Hat Enterprise 6.3
  • MySQL 5.5.28
  • 8GB RAM