Я пробовал разные коды с @Suspended Final Asyncresponse, дает внутренний сервер 500. Локальный сервер: WebSphere Liberty < /p>
без @sussded Я включил comurrent-1.0 in server.xml.
Я использую http: // localhost: 9080/cdiexecitemstime от restclient, чтобы назвать API.
my classe as As Ase:
my classe:
.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 result = itemService.listItems();
Response resp = Response.ok(result).build();
ar.resume(resp);
};
executor.submit(r);
}
}
< /code>
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 listItems() {
Collection 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;
}
}
< /code>
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;
}
< /code>
} < /h2>
Я проверил, что в консоли сервера нет журнала напечатано для ошибки.
Когда я упоминаю "@souscent final asyncresponse" < /strong> evowerling getItems () < /strong> method i. Он напрямую показывает ошибку внутренней сервера, т. Е. 500 В ответ на ошибку ниже. < /P>
тело ответа отображается ниже ошибкой: < /h2>
"An error occurred while processing a request."
< /code>
Заголовки ответов, как ниже: < /h2>
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
< /code>
Here in response header, I looked two strange things as below:
- connection is showing closed.
- Content-Type is showing application/octet-stream.
Though I mentioned mediatype is JSON for below API resource method
@Produces(MediaType.APPLICATION_JSON)
public void getItems(@Suspended final AsyncResponse ar)
Подробнее здесь: https://stackoverflow.com/questions/460 ... -error-500