2

环境:

  • NetBeans 8.0.2
  • 玻璃鱼 4.1
  • 泰鲁斯 1.11

我正在尝试在http://localhost:8080/doit/changes中测试 websocket,但我正在测试javax.websocket.DeploymentException

ToDoChangeTracker.java

...
import java.io.IOException;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Singleton;
import javax.enterprise.event.Observes;
import javax.enterprise.event.TransactionPhase;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@Singleton
@ServerEndpoint("/changes")
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class ToDoChangeTracker {

    private Session session;

    @OnOpen
    public void onOpen(Session session){
        this.session = session;
    }

    @OnClose
    public void onClose(Session session){
        this.session = null;
    }

    public void onToDoChange(@Observes(during = TransactionPhase.AFTER_SUCCESS) ToDo todo){
        //System.out.println("################# todo changed and committed = " + todo);
        if(this.session != null && this.session.isOpen()){
            try {
                this.session.getBasicRemote().sendText(todo.toString());
            } catch (IOException ex) {
                //we ignore this
            }
        }
    }

}

ToDoChangesTrackerIT.java

    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import javax.websocket.ContainerProvider;
    import javax.websocket.DeploymentException;
    import javax.websocket.WebSocketContainer;
    import static org.junit.Assert.assertNotNull;
    import org.junit.Before;
    import org.junit.Test;

    /**
     *
     * @author jose
     */
    public class ToDoChangeTrackerIT {
        private WebSocketContainer container;
        private ChangesListener listener;

        @Before
        public void initContainer() throws URISyntaxException, DeploymentException, IOException{
            this.container = ContainerProvider.getWebSocketContainer();
            URI uri = new URI("ws://localhost:8080/doit/changes");
            this.listener = new ChangesListener();
            this.container.connectToServer(container, uri); //Error
        }

        @Test
        public void receiveNotifications() throws InterruptedException{
            String message = this.listener.getMessage();
            assertNotNull(message);
            System.out.println(" " + message);
        }
    }

我创建了一个 javascript 文件来测试 websocket,它确实有效,结果是

连接到 ws://localhost:8080/doit/changes

websocket.js

var wsUri = "ws://" + document.location.host + document.location.pathname + "changes";
var websocket = new WebSocket(wsUri);

websocket.onerror = function(evt) { onError(evt) };

function onError(evt) {
    writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}

// For testing purposes
var output = document.getElementById("output");
websocket.onopen = function(evt) { onOpen(evt) };

function writeToScreen(message) {
    output.innerHTML += message + "<br>";
}

function onOpen() {
    writeToScreen("Connected to " + wsUri);
}
// End test functions

错误

javax.websocket.DeploymentException
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:383)
    at com.airhacks.doit.business.reminders.boundary.ToDoChangeTrackerIT.initContainer(ToDoChangeTrackerIT.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.NullPointerException
    at org.glassfish.tyrus.core.AnnotatedEndpoint.<init>(AnnotatedEndpoint.java:161)
    at org.glassfish.tyrus.core.AnnotatedEndpoint.fromInstance(AnnotatedEndpoint.java:135)
    at org.glassfish.tyrus.client.ClientManager$3.run(ClientManager.java:559)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.glassfish.tyrus.client.ClientManager$SameThreadExecutorService.execute(ClientManager.java:866)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:511)
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:373)
    ... 30 more

任何想法?

4

0 回答 0