Jax-Rs @Suspended最终AsyncResponse给出了内部服务器错误500

时间:2017-09-05 12:01:14

标签: java multithreading rest asynchronous jax-rs

我是Jaxrs restful异步编程的初学者。

我尝试使用@Suspended的不同代码最终AsyncResponse给出了内部服务器错误500.本地服务器:Websphere Liberty

没有@Suspended最终的AsyncResponse同步应用程序工作正常。我在server.xml中启用了<feature>concurrent-1.0</feature>

我正在使用restclient中的http://localhost:9080/cdiexecitemsTimeout来调用api。

我的课程如下:

import java.util.Collection;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.CompletionCallback;
import javax.ws.rs.container.ConnectionCallback;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.container.TimeoutHandler;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("cdiexecitemsTimeout")
public class ItemsCDIExecutorResourceTimeout {

    ItemService itemService = new ItemService();

    @Resource
    ManagedExecutorService executor;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public void getItems(@Suspended final AsyncResponse ar) {
        System.out.println("cdiexecitems: " + executor);

        ar.setTimeout(500, TimeUnit.MILLISECONDS);
        ar.setTimeoutHandler(new TimeoutHandler() {
            public void handleTimeout(AsyncResponse arg0) {
                ar.resume(Response.ok("Backup plan! " + new Item().getId()).build());
            }
        });

        ar.register(new CompletionCallback() {
            @Override
            public void onComplete(Throwable t) {
                System.out.println("DONE! ");
                if ( t != null ) {
                    t.printStackTrace();
                }
            }
        });

        ar.register(new ConnectionCallback() {
            @Override
            public void onDisconnect(AsyncResponse ar) {
                System.out.println("Disconnected: " + ar);
            }
        });

        Runnable r = () -> {
            Collection<Item> result = itemService.listItems();
            Response resp = Response.ok(result).build();
            ar.resume(resp);
        };

        executor.submit(r);
    }
}
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

public class ItemService {

    /**
     * "Long-running" list operation
     * @return Collection containing two items
     */
    public Collection<Item> listItems() {
        Collection<Item> result = Arrays.asList(new Item(), new Item());
        try {
            System.out.println("Wait for 30 seconds before returning " + result);
            Thread.sleep(TimeUnit.SECONDS.toMillis(30));
        } catch (InterruptedException e) {
        }

        // The list always contains 2 items, that will have incrementing ids
        return result;
    }

    public void addItem(Item newItem) {
        System.out.println("New items added");
    }

    public Item get(int id) {
        return null;
    }
}
import java.util.concurrent.atomic.AtomicInteger;

public class Item {
    private final static AtomicInteger next = new AtomicInteger();

    private long id;

    public Item() {
        id = next.incrementAndGet();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String toString() {
        return "item-"+id;
    }

}

我检查过在服务器控制台中没有为错误打印日志。 当我提到“@ Suspended final AsyncResponse”甚至第一行的 getItems()方法时,即System.out.println()语句没有执行。它直接显示内部服务器错误,即500响应以下错误。

响应正文显示以下错误:

"<error>An error occurred while processing a request.</error>"

响应标头如下:

Status Code: 500 Internal Server Error
Access-Control-Allow-Headers: Origin, Accept, x-auth-token, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, userid,subscr
iberid,applicationicid
Access-Control-Allow-Methods: POST, GET, HEAD, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin: *
Cache-Control: no-cache="set-cookie, set-cookie2"
Connection: Close
Content-Length: 60
Content-Type: application/octet-stream
Date: Wed, 06 Sep 2017 18:30:17 GMT
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Set-Cookie: JSESSIONID=0000zgpDbKh-h8Ej7qWFwzrgqXk:5d00ad20-2db0-463a-9c76-a6ba0eb76dcc; Path=/; HttpOnly
X-Content-Type-Options: nosniff
X-Powered-By: Servlet/3.1

在回复标题中,我看了两个奇怪的事情如下:

  1. 连接显示已关闭。
  2. Content-Type显示 application / octet-stream。 虽然我提到mediatype对于以下API资源方法 JSON

    @Produces(MediaType.APPLICATION_JSON)  public void getItems(@Suspended final AsyncResponse ar)

  3. 请你帮我解决确切的错误。

0 个答案:

没有答案