java.nio.channels.ClosedChannelException HTTP / 2 with码头

问题描述 投票:2回答:1

我正在尝试使用HTTP / 2向生产中的设备发送苹果通知。

我正在将此-Xbootclasspath / p:/home/mohamed/Desktop/alpn-boot-8.1.9.v20160720.jar作为Default VM arguments在Eclipse中传递。

这里是我现在正在使用的代码:

public static void pushoNotification() {
    try {
        // create a low-level Jetty HTTP/2 client
        HTTP2Client lowLevelClient = new HTTP2Client();
        lowLevelClient.start();

        // APNs requires the use of HPACK (header compression for HTTP/2), which prevents repeated header keys and values.
        KeyStore ks = KeyStore.getInstance("PKCS12");

        // Ensure that the password is the same as the one used later in setKeyStorePassword()
        ks.load(PushNotifications.class.getClassLoader().getResourceAsStream("Prod2.p12"), "a12B34".toCharArray());

        SslContextFactory ssl = new SslContextFactory(true);
        ssl.setKeyStore(ks);
        ssl.setKeyStorePassword("a12B34");

        // create a high-level Jetty client
        HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(lowLevelClient), ssl);
        client.start();

        // request-response exchange
        ContentResponse response = client.POST("https://api.push.apple.com").path("/3/device/19297dba97212ac6fd16b9cd50f2d86629aed0e49576b2b52ed05086087da802")
                .content(new StringContentProvider("{ \"aps\" : { \"alert\" : \"Hello\" } }")).send();
        response.toString();
        client.stop();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我的依赖项

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jetty.http2</groupId>
        <artifactId>http2-http-client-transport</artifactId>
        <version>9.4.0.M1</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-http</artifactId>
        <version>9.4.0.M1</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jetty.http2</groupId>
        <artifactId>http2-client</artifactId>
        <version>9.4.0.M1</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-client</artifactId>
        <version>9.4.0.M1</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-alpn-client</artifactId>
        <version>9.4.0.M1</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.mortbay.jetty.alpn</groupId>
        <artifactId>alpn-boot</artifactId>
        <version>8.1.9.v20160720</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.2</version>
    </dependency>

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.4.1</version>
    </dependency>

但是当我从main方法运行该方法时,它抛出java.nio.channels.ClosedChannelException

我也在Eclipse控制台中得到了这个INFO org.eclipse.jetty.http2.HTTP2Session-通知侦听器org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2$SessionListenerPromise@87eaf9c时失败。>

任何想法?

谢谢

我正在尝试使用HTTP / 2向生产中的设备发送苹果通知。我通过了-Xbootclasspath / p:/home/mohamed/Desktop/alpn-boot-8.1.9.v20160720.jar作为默认VM参数...

java push-notification apple-push-notifications jetty-9 javapns
1个回答
0
投票

我在大约3年零6个月后粘贴我的答案,只是为了帮助任何人得到相同的错误,实际上我不记得我是如何解决此问题的,但是这里是我现在正在使用的有效代码。 。

© www.soinside.com 2019 - 2024. All rights reserved.