Atmosphere Jersey资源调用永远不会触发WebSocketTextListener的OnMessage方法

时间:2012-07-20 12:00:56

标签: websocket jersey atmosphere

我已经用Atmosphere& amp;编写了两个junit方法来测试我的Jersey资源。的WebSockets。

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

知道没有调用OnMessage方法的原因吗?

Atmosphere Jersey资源:

@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 CALL:

 @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 BROADCAST WEBSOCKET CALL:

 @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();

    }

执行第一次SUSPEND CALL然后执行BRODCAST调用时的Jersey日志:

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 

1 个答案:

答案 0 :(得分:1)

问题是由于两个不同的AsyncHttpClient用于SUBSCRIBE和BROADCAST调用。

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

 private AsyncHttpClient client;

    @Before
    public void setUp() throws Exception {

         client = new AsyncHttpClient();

    }