Java 想要使用jersey 2的restful API进行长时间轮询吗

Java 想要使用jersey 2的restful API进行长时间轮询吗,java,rest,long-polling,jersey-2.0,jersey-client,Java,Rest,Long Polling,Jersey 2.0,Jersey Client,我的要求: 我想使API类似于dropbox的。实际上,我想在服务器上发现更改时通知所有客户端。所以服务器可能需要2小时或更长时间,然后通知其所有客户端我有一些更新。为了安全起见,我不想使用comet、websocket等 1> 建议我如何制作这样的API 我的努力: 我创造了 服务器代码: @GET @Path("/longpoll") @ManagedAsync public void async(@Suspended final AsyncResponse asyn

我的要求:

我想使API类似于dropbox的
。实际上,我想在服务器上发现更改时通知所有客户端。所以服务器可能需要2小时或更长时间,然后通知其所有客户端我有一些更新。为了安全起见,我不想使用
comet
websocket

1> 建议我如何制作这样的API

我的努力: 我创造了

服务器代码:

@GET
    @Path("/longpoll")
    @ManagedAsync
    public void async(@Suspended final AsyncResponse asyncResponse)
    {
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                String result = veryExpensiveOperation();
                asyncResponse.resume(result);
            }

            private String veryExpensiveOperation()
            {
                try
                {
                    // put some long running code here
                    Thread.sleep(60000);
                    return "Hello World";
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    return "error";
                }

            }
        }).start();
    }
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;

public class JersyClient
{
    public static void main(String args[]) throws InterruptedException, ExecutionException
    {
        ClientConfig clientConfig = new ClientConfig();
        Client client = ClientBuilder.newClient(clientConfig);
        WebTarget target = client.target("http://localhost:8080");
        Invocation.Builder invocationBuilder = target.path("MyApp_3_3_5/api/1/longpoll").request();
        invocationBuilder.header("Authorization", "Bearer yBOrnV6zlOrgoYxrMsfQ_BYZ5p37gALB");
        final Future<Response> future = invocationBuilder.async().get(new InvocationCallback<Response>()
        {
            @Override
            public void completed(Response response)
            {
                System.out.println("Response status code "
                        + response.getStatus() + " received.");
                System.out.println("In response " + response.readEntity(String.class));
            }

            @Override
            public void failed(Throwable throwable)
            {
                System.out.println("Invocation failed.");
                throwable.printStackTrace();
            }
        });

        Response response = future.get();
        System.out.println("your response " + response.readEntity(String.class));
        // get() waits for the response to be ready
        System.out.println("Go ahead");
    }
}
现在为了测试这个,我创建了一个客户端

客户端代码:

@GET
    @Path("/longpoll")
    @ManagedAsync
    public void async(@Suspended final AsyncResponse asyncResponse)
    {
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                String result = veryExpensiveOperation();
                asyncResponse.resume(result);
            }

            private String veryExpensiveOperation()
            {
                try
                {
                    // put some long running code here
                    Thread.sleep(60000);
                    return "Hello World";
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    return "error";
                }

            }
        }).start();
    }
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;

public class JersyClient
{
    public static void main(String args[]) throws InterruptedException, ExecutionException
    {
        ClientConfig clientConfig = new ClientConfig();
        Client client = ClientBuilder.newClient(clientConfig);
        WebTarget target = client.target("http://localhost:8080");
        Invocation.Builder invocationBuilder = target.path("MyApp_3_3_5/api/1/longpoll").request();
        invocationBuilder.header("Authorization", "Bearer yBOrnV6zlOrgoYxrMsfQ_BYZ5p37gALB");
        final Future<Response> future = invocationBuilder.async().get(new InvocationCallback<Response>()
        {
            @Override
            public void completed(Response response)
            {
                System.out.println("Response status code "
                        + response.getStatus() + " received.");
                System.out.println("In response " + response.readEntity(String.class));
            }

            @Override
            public void failed(Throwable throwable)
            {
                System.out.println("Invocation failed.");
                throwable.printStackTrace();
            }
        });

        Response response = future.get();
        System.out.println("your response " + response.readEntity(String.class));
        // get() waits for the response to be ready
        System.out.println("Go ahead");
    }
}
所以在这里,客户端不会等待服务器在1分钟后发送的实际响应

  • 服务器代码有问题吗
  • 为什么客户不等待实际响应
  • 我应该用这种方式实现长时间池吗
  • 如果服务器在2小时后发送响应,那么在这种情况下是否有效