Azure / azure-relay-java

Azure Relay Java SDK
MIT License
8 stars 9 forks source link

The library doesn't work properly with Tomcat server #51

Open GalynaY opened 5 years ago

GalynaY commented 5 years ago

HybridConnectionListener throws exceptions when using it on a tomcat:

19-Jul-2019 13:48:07.248 SEVERE [autoshutdown-pool-1-thread-3] com.microsoft.azure.relay.RelayLogger.throwingException ClientWebSocket(TrackingId:de9dd33c-7433-49c0-978d-07e191f35798_G16_G10, Address:sb://statelink-relay.servicebus.windows.net) is throwing an Exception: javax.websocket.DeploymentException: Cannot use POJO class [com.microsoft.azure.relay.ClientWebSocket] as it is not annotated with @ClientEndpoint at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:124) at com.microsoft.azure.relay.ClientWebSocket.lambda$connectAsync$0(ClientWebSocket.java:121) at com.microsoft.azure.relay.CompletableFutureUtil.lambda$futureToCompletableFuture$1(CompletableFutureUtil.java:62) at com.microsoft.azure.relay.AutoShutdownScheduledExecutor.lambda$submit$0(AutoShutdownScheduledExecutor.java:60) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 19-Jul-2019 13:48:07.250 SEVERE [autoshutdown-pool-1-thread-3] com.microsoft.azure.relay.RelayLogger.throwingException HybridConnectionListener(TrackingId:92aa61bb-a431-4230-aa5f-a56bf29d0960, Address:sb://statelink-relay.servicebus.windows.net) is throwing an Exception: java.util.concurrent.CompletionException: java.lang.RuntimeException: javax.websocket.DeploymentException: Cannot use POJO class [com.microsoft.azure.relay.ClientWebSocket] as it is not annotated with @ClientEndpoint at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292) at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308) at java.util.concurrent.CompletableFuture.uniRun(CompletableFuture.java:700) at java.util.concurrent.CompletableFuture$UniRun.tryFire(CompletableFuture.java:687) at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474) at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977) at com.microsoft.azure.relay.CompletableFutureUtil.lambda$futureToCompletableFuture$1(CompletableFutureUtil.java:68) at com.microsoft.azure.relay.AutoShutdownScheduledExecutor.lambda$submit$0(AutoShutdownScheduledExecutor.java:60) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.RuntimeException: javax.websocket.DeploymentException: Cannot use POJO class [com.microsoft.azure.relay.ClientWebSocket] as it is not annotated with @ClientEndpoint at com.microsoft.azure.relay.RelayLogger.throwingException(RelayLogger.java:77) at com.microsoft.azure.relay.RelayLogger.throwingException(RelayLogger.java:53) at com.microsoft.azure.relay.ClientWebSocket.lambda$connectAsync$0(ClientWebSocket.java:127) at com.microsoft.azure.relay.CompletableFutureUtil.lambda$futureToCompletableFuture$1(CompletableFutureUtil.java:62) ... 8 more

If I add an annotation, receives other ClassNotFoundException and ClassCastException. I'd like to be able to use this library on any server not only jetty

bainian12345 commented 5 years ago

@GalynaY Hi, what dependency or configurations are you running this library with? And would you mind sharing some code that leads to the exceptions that you are seeing?

GalynaY commented 5 years ago

I run your sample built-in web application so I can deploy it on a tomcat: ` import com.microsoft.azure.relay.HybridConnectionListener; import com.microsoft.azure.relay.RelayConnectionStringBuilder; import com.microsoft.azure.relay.TokenProvider; import org.joda.time.DateTime;

import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer;

public class WebsocketListener {

private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";

public void startListening() throws URISyntaxException {

    String connectionName = System.getenv("connectionName");
    String url = System.getenv("url");
    String key = System.getenv("key");
    String connectionString = String.format("Endpoint=sb://%s/%s;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=%s", url, connectionName, key);
    System.out.println(connectionString);
    RelayConnectionStringBuilder connectionParams = new RelayConnectionStringBuilder(connectionString);

    TokenProvider tokenProvider = TokenProvider.createSharedAccessSignatureTokenProvider(
            connectionParams.getSharedAccessKeyName(), connectionParams.getSharedAccessKey());
    HybridConnectionListener listener = new HybridConnectionListener(new URI(connectionParams.getEndpoint().toString()), tokenProvider);

    listener.openAsync().join();

    while (listener.isOnline()) {

        // If listener closes, then listener.acceptConnectionAsync() will complete with null after closing down
        listener.acceptConnectionAsync().thenAccept(connection -> {
            // connection may be null if the listener is closed before receiving a connection
            if (connection != null) {
                System.out.println("New session connected.");

                while (connection.isOpen()) {
                    ByteBuffer bytesReceived = connection.readAsync().join();
                    // If the read operation is still pending when connection closes, the read result as null.
                    if (bytesReceived.remaining() > 0) {
                        String msg = new String(bytesReceived.array(), bytesReceived.arrayOffset(), bytesReceived.remaining());
                        String response = "Message received";

                        ByteBuffer msgToSend = ByteBuffer.wrap(response.getBytes());

                        System.out.println("Received: " + msg);
                        System.out.println("Received file at: " + DateTime.now().toString(TIMESTAMP_FORMAT));
                        connection.writeAsync(msgToSend);
                    }
                }
                System.out.println("Session disconnected.");
            }
        }).join();
    }
}

}`

The simple servlet:

`

import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URISyntaxException;

@WebServlet( name = "test", urlPatterns = "/test" ) public class SampleServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpListener listener = new HttpListener();
    try {
        listener.doS();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

} `

and pom: `<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0
<groupId>face</groupId>
<artifactId>face</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-relay</artifactId>
        <version>0.0.2-PREVIEW</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-websocket</artifactId>
        <version>8.5.43</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-api</artifactId>
        <version>8.5.43</version>
    </dependency>

</dependencies>

<build>
    <finalName>SampleServlet</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
            </configuration>
        </plugin>
    </plugins>
</build>

`

jfggdl commented 5 years ago

@GalynaY, thanks for creating this issue. We have added this request to our backlog. We will continue working in the root cause of this problem according to priorities.