在WebSocketTextListener的Message方法上从不激发资源调用

在WebSocketTextListener的Message方法上从不激发资源调用,websocket,jersey,atmosphere,Websocket,Jersey,Atmosphere,我已经编写了两个junit方法来使用Atmosphere&webSockets测试我的Jersey资源 问题在于,当我调用Suspend和Broacast时,只调用WebSocketTextListener的onOpen方法。OnError、OnMessage和OnClose都不被调用:( 知道为什么不调用OnMessage方法吗 大气资源: @Path("/websocket") @Suspend @GET @Produces({MediaType.APPLICATIO

我已经编写了两个junit方法来使用Atmosphere&webSockets测试我的Jersey资源

问题在于,当我调用Suspend和Broacast时,只调用WebSocketTextListener的onOpen方法。OnError、OnMessage和OnClose都不被调用:(

知道为什么不调用OnMessage方法吗

大气资源:

@Path("/websocket")
    @Suspend
    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public String suspend() {
        return "";
    }   

    @Path("/websocket")
    @Broadcast(writeEntity = false)
    @POST
    @Produces({MediaType.APPLICATION_JSON})
    public String broadcast(String message) {
        return "BROADCASTTT";
    }  
测试挂起WEBSOCKET调用:

 @Test
    public void testAddMealSubscriber() throws Exception {

        final CountDownLatch latch = new CountDownLatch(1);
        String restaurantId = "SalernoNapoliBarcelona";
        String mealId = "14b74bddc68d6f1b4c22e7f7b200067f";

        String url = "ws://localhost:8080/rest/" + "restaurants/" + restaurantId + "/meals/" + mealId + "/websocket/";

        AsyncHttpClient client = new AsyncHttpClient();

        try {
            final AtomicReference response = new AtomicReference(null);
            WebSocket websocket = client.prepareGet(url)
                    .execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
                            new WebSocketTextListener() {

                                @Override
                                public void onMessage(String message) {
                                    System.out.println("WebSocketTextListener onMessage:" + message);
                                    response.set(message);
                                    latch.countDown();
                                }

                                @Override
                                public void onFragment(String fragment, boolean last) {

                                    System.out.println("WebSocketTextListener onFragment:" + fragment);
                                }

                                @Override
                                public void onOpen(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onOpen");
                                }

                                @Override
                                public void onClose(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onClose");
                                    latch.countDown();
                                }

                                @Override
                                public void onError(Throwable t) {
                                    System.out.println("WebSocketTextListener onError");
                                    t.printStackTrace();
                                }
                            }).build()).get();

            try {
                latch.await(60, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            assertNotNull(response.get());
            assertEquals(response.get(), "echo");
        } catch (Exception e) {
            e.printStackTrace();
        }

        client.close();

    }
 @Test
    public void testAddMealPublisher() throws Exception {

        final CountDownLatch latch = new CountDownLatch(1);
        String restaurantId = "SalernoNapoliBarcelona";
        String mealId = "14b74bddc68d6f1b4c22e7f7b200067f";

        String url = "ws://localhost:8080/rest/" + "restaurants/" + restaurantId + "/meals/" + mealId + "/websocket/";

        AsyncHttpClient c = new AsyncHttpClient();
        try {
            final AtomicReference response = new AtomicReference(null);

            WebSocket websocket = c.prepareGet(url)
                    .execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
                            new WebSocketTextListener() {

                                @Override
                                public void onMessage(String message) {
                                    response.set(message);
                                    latch.countDown();
                                }

                                @Override
                                public void onFragment(String fragment, boolean last) {

                                    System.out.println("WebSocketTextListener onFragment:" + fragment);
                                }

                                @Override
                                public void onOpen(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onOpen");
                                }

                                @Override
                                public void onClose(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onClose");
                                    latch.countDown();
                                }

                                @Override
                                public void onError(Throwable t) {
                                    System.out.println("WebSocketTextListener onError");
                                    t.printStackTrace();
                                }
                            }).build()).get().sendTextMessage("MESSSAGGGEEEE");


            try {
                latch.await(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            assertNotNull(response.get());
            assertEquals(response.get(), "echo");

        } catch (Exception e) {
            e.printStackTrace();
        }

        c.close();

    }
测试广播WEBSOCKET调用:

 @Test
    public void testAddMealSubscriber() throws Exception {

        final CountDownLatch latch = new CountDownLatch(1);
        String restaurantId = "SalernoNapoliBarcelona";
        String mealId = "14b74bddc68d6f1b4c22e7f7b200067f";

        String url = "ws://localhost:8080/rest/" + "restaurants/" + restaurantId + "/meals/" + mealId + "/websocket/";

        AsyncHttpClient client = new AsyncHttpClient();

        try {
            final AtomicReference response = new AtomicReference(null);
            WebSocket websocket = client.prepareGet(url)
                    .execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
                            new WebSocketTextListener() {

                                @Override
                                public void onMessage(String message) {
                                    System.out.println("WebSocketTextListener onMessage:" + message);
                                    response.set(message);
                                    latch.countDown();
                                }

                                @Override
                                public void onFragment(String fragment, boolean last) {

                                    System.out.println("WebSocketTextListener onFragment:" + fragment);
                                }

                                @Override
                                public void onOpen(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onOpen");
                                }

                                @Override
                                public void onClose(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onClose");
                                    latch.countDown();
                                }

                                @Override
                                public void onError(Throwable t) {
                                    System.out.println("WebSocketTextListener onError");
                                    t.printStackTrace();
                                }
                            }).build()).get();

            try {
                latch.await(60, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            assertNotNull(response.get());
            assertEquals(response.get(), "echo");
        } catch (Exception e) {
            e.printStackTrace();
        }

        client.close();

    }
 @Test
    public void testAddMealPublisher() throws Exception {

        final CountDownLatch latch = new CountDownLatch(1);
        String restaurantId = "SalernoNapoliBarcelona";
        String mealId = "14b74bddc68d6f1b4c22e7f7b200067f";

        String url = "ws://localhost:8080/rest/" + "restaurants/" + restaurantId + "/meals/" + mealId + "/websocket/";

        AsyncHttpClient c = new AsyncHttpClient();
        try {
            final AtomicReference response = new AtomicReference(null);

            WebSocket websocket = c.prepareGet(url)
                    .execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
                            new WebSocketTextListener() {

                                @Override
                                public void onMessage(String message) {
                                    response.set(message);
                                    latch.countDown();
                                }

                                @Override
                                public void onFragment(String fragment, boolean last) {

                                    System.out.println("WebSocketTextListener onFragment:" + fragment);
                                }

                                @Override
                                public void onOpen(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onOpen");
                                }

                                @Override
                                public void onClose(WebSocket websocket) {
                                    System.out.println("WebSocketTextListener onClose");
                                    latch.countDown();
                                }

                                @Override
                                public void onError(Throwable t) {
                                    System.out.println("WebSocketTextListener onError");
                                    t.printStackTrace();
                                }
                            }).build()).get().sendTextMessage("MESSSAGGGEEEE");


            try {
                latch.await(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            assertNotNull(response.get());
            assertEquals(response.get(), "echo");

        } catch (Exception e) {
            e.printStackTrace();
        }

        c.close();

    }
执行第一个挂起调用和第二个BRODCAST调用时的日志:

Jul 20, 2012 1:54:10 PM com.sun.jersey.api.container.filter.LoggingFilter filter INFO: 1 * Server in-bound request 1 > GET http://localhost:8080/rest/restaurants/SalernoNapoliBarcelona/meals/14b74bddc68d6f1b4c22e7f7b200067f/websocket/ 1 > Sec-WebSocket-Version: 13 1 > Upgrade: WebSocket 1 > Sec-WebSocket-Key: Wf7vyIGCD3Sa8StcdsGIkg== 1 > Host: localhost:8080 1 > Accept: */* 1 > User-Agent: NING/1.0 1 > Connection: Upgrade 1 > Origin: http://localhost:8080 1 > X-Atmosphere-Transport: websocket 1 > Jul 20, 2012 1:54:31 PM com.sun.jersey.api.container.filter.LoggingFilter filter INFO: 2 * Server in-bound request 2 > GET http://localhost:8080/rest/restaurants/SalernoNapoliBarcelona/meals/14b74bddc68d6f1b4c22e7f7b200067f/websocket/ 2 > Sec-WebSocket-Version: 13 2 > Upgrade: WebSocket 2 > Sec-WebSocket-Key: RH/DbdkwQK1xBwhyhXLkAQ== 2 > Host: localhost:8080 2 > Accept: */* 2 > User-Agent: NING/1.0 2 > Connection: Upgrade 2 > Origin: http://localhost:8080 2 > X-Atmosphere-Transport: websocket 2 > Jul 20, 2012 1:54:34 PM com.sun.jersey.api.container.filter.LoggingFilter filter INFO: 3 * Server in-bound request 3 > POST http://localhost:8080/rest/restaurants/SalernoNapoliBarcelona/meals/14b74bddc68d6f1b4c22e7f7b200067f/websocket/ 3 > X-Atmosphere-Transport: websocket 3 > X-Atmosphere-Transport: websocket 3 > Content-Type: application/json 3 > Jul 20, 2012 1:54:34 PM com.sun.jersey.api.container.filter.LoggingFilter$Adapter finish INFO: 3 * Server out-bound response 3 2012年7月20日下午1:54:10 com.sun.jersey.api.container.filter.LoggingFilter 信息:1*绑定请求中的服务器 1>获取http://localhost:8080/rest/restaurants/SalernoNapoliBarcelona/meals/14b74bddc68d6f1b4c22e7f7b200067f/websocket/ 1>秒WebSocket版本:13 1>升级:WebSocket 1>秒WebSocket键:Wf7vyIGCD3Sa8StcdsGIkg== 1>主机:本地主机:8080 1>接受:*/* 1>用户代理:NING/1.0 1>连接:升级 1>来源:http://localhost:8080 1>X-大气传输:websocket 1 > 2012年7月20日下午1:54:31 com.sun.jersey.api.container.filter.LoggingFilter 信息:2*绑定请求中的服务器 2>获得http://localhost:8080/rest/restaurants/SalernoNapoliBarcelona/meals/14b74bddc68d6f1b4c22e7f7b200067f/websocket/ 2>Sec WebSocket版本:13 2>升级:WebSocket 2>秒腹板锁键:RH/DbdkwQK1xBwhyhXLkAQ== 2>主机:本地主机:8080 2>接受:*/* 2>用户代理:NING/1.0 2>连接:升级 2>来源:http://localhost:8080 2>X-大气传输:websocket 2 > 2012年7月20日下午1:54:34 com.sun.jersey.api.container.filter.LoggingFilter 信息:绑定请求中的3*服务器 3>职位http://localhost:8080/rest/restaurants/SalernoNapoliBarcelona/meals/14b74bddc68d6f1b4c22e7f7b200067f/websocket/ 3>X-大气传输:websocket 3>X-大气传输:websocket 3>内容类型:应用程序/json 3 > 2012年7月20日下午1:54:34 com.sun.jersey.api.container.filter.LoggingFilter$适配器完成 信息:3*服务器出界响应
3问题是由于订阅和广播调用使用了两个不同的AsyncHttpClient

要解决此问题,请在SetUp方法中仅创建一个AsyncHttpClient,并在两种测试方法中使用它:

 private AsyncHttpClient client;

    @Before
    public void setUp() throws Exception {

         client = new AsyncHttpClient();

    }

问题的原因是,订阅和广播调用使用了两个不同的AsyncHttpClient

要解决此问题,请在SetUp方法中仅创建一个AsyncHttpClient,并在两种测试方法中使用它:

 private AsyncHttpClient client;

    @Before
    public void setUp() throws Exception {

         client = new AsyncHttpClient();

    }