quarks.connectors.wsclient.javax.websocket

Class Jsr356WebSocketClient

  • java.lang.Object
    • quarks.connectors.wsclient.javax.websocket.Jsr356WebSocketClient
  • All Implemented Interfaces:
    WebSocketClient, TopologyElement


    public class Jsr356WebSocketClient
    extends java.lang.Object
    implements WebSocketClient
    A connector for sending and receiving messages to a WebSocket Server.

    A connector is bound to its configuration specified javax.websockets WebSocket URI.

    A single connector instance supports sinking at most one stream and sourcing at most one stream.

    Sample use:

    
     // assuming a properties file containing at least:
     // ws.uri=ws://myWsServerHost/myService
      
     String propsPath = <path to properties file>; 
     Properties properties = new Properties();
     properties.load(Files.newBufferedReader(new File(propsPath).toPath()));
    
     Topology t = ...;
     Jsr356WebSocketClient wsclient = new Jsr356WebSocketClient(t, properties);
     
     // send a stream's JsonObject tuples as JSON WebSocket text messages
     TStream<JsonObject> s = ...;
     wsclient.send(s);
     
     // create a stream of JsonObject tuples from received JSON WebSocket text messages
     TStream<JsonObject> r = wsclient.receive();
     r.print();
     

    Note, the WebSocket protocol differentiates between text/String and binary/byte messages. A receiver only receives the messages of the type that it requests.

    The connector is written against the JSR356 javax.websockets API. javax.websockets uses the ServiceLoader to load an implementation of javax.websocket.ContainerProvider.

    The supplied connectors/javax.websocket-client provides one such implementation. To use it, include connectors/javax.websocket-client/lib/javax.websocket-client.jar on your classpath.

    • Constructor Summary

      Constructors 
      Constructor and Description
      Jsr356WebSocketClient(Topology t, java.util.Properties config, Supplier<javax.websocket.WebSocketContainer> containerFn)
      Create a new Web Socket Client connector.
      Jsr356WebSocketClient(Topology t, java.util.Properties config)
      Create a new Web Socket Client connector.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      TStream<com.google.gson.JsonObject> receive()
      Create a stream of JsonObject tuples from received JSON WebSocket text messages.
      TStream<byte[]> receiveBytes()
      Create a stream of byte[] tuples from received WebSocket binary messages.
      TStream<java.lang.String> receiveString()
      Create a stream of String tuples from received WebSocket text messages.
      TSink<com.google.gson.JsonObject> send(TStream<com.google.gson.JsonObject> stream)
      Send a stream's JsonObject tuples as JSON in a WebSocket text message.
      TSink<byte[]> sendBytes(TStream<byte[]> stream)
      Send a stream's byte[] tuples in a WebSocket binary message.
      TSink<java.lang.String> sendString(TStream<java.lang.String> stream)
      Send a stream's String tuples in a WebSocket text message.
      Topology topology()
      Topology this element is contained in.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Jsr356WebSocketClient

        public Jsr356WebSocketClient(Topology t,
                                     java.util.Properties config)
        Create a new Web Socket Client connector.

        Configuration parameters:

        • ws.uri - "ws://host[:port][/path]", "wss://host[:port][/path]" the default port is 80 and 443 for "ws" and "wss" respectively. The optional path must match the server's configuration.
        • ws.trustStore - optional. Only used with "wss:". Path to trust store file in JKS format. If not set, the standard JRE and javax.net.ssl system properties control the SSL behavior. Generally not required if server has a CA-signed certificate.
        • ws.trustStorePassword - required if ws.trustStore is set
        • ws.keyStore - optional. Only used with "wss:" when the server is configured for client auth. Path to key store file in JKS format. If not set, the standard JRE and javax.net.ssl system properties control the SSL behavior.
        • ws.keyStorePassword - required if ws.keyStore is set.
        • ws.keyPassword - defaults to ws.keyStorePassword value
        • ws.keyCertificateAlias - alias for certificate in key store. defaults to "default"
        Additional keys in config are ignored.
        Parameters:
        t - the topology to add the connector to
        config - the connector's configuration
      • Jsr356WebSocketClient

        public Jsr356WebSocketClient(Topology t,
                                     java.util.Properties config,
                                     Supplier<javax.websocket.WebSocketContainer> containerFn)
        Create a new Web Socket Client connector.

        This constructor is made available in case the container created by Jsr356WebSocketClient(Topology, Properties) lacks the configuration needed for a particular use case.

        At topology runtime containerFn.get() will be called to get a javax.websocket.WebSocketContainer that will be used to connect to the WebSocket server.

        Only the "ws.uri" config parameter is used.

        Parameters:
        t - the topology to add the connector to
        config - the connector's configuration
        containerFn - supplier for a WebSocketContainer. May be null.
    • Method Detail

      • send

        public TSink<com.google.gson.JsonObject> send(TStream<com.google.gson.JsonObject> stream)
        Send a stream's JsonObject tuples as JSON in a WebSocket text message.
        Specified by:
        send in interface WebSocketClient
        Parameters:
        stream - the stream
        Returns:
        sink
      • sendString

        public TSink<java.lang.String> sendString(TStream<java.lang.String> stream)
        Send a stream's String tuples in a WebSocket text message.
        Specified by:
        sendString in interface WebSocketClient
        Parameters:
        stream - the stream
        Returns:
        sink
      • sendBytes

        public TSink<byte[]> sendBytes(TStream<byte[]> stream)
        Send a stream's byte[] tuples in a WebSocket binary message.
        Specified by:
        sendBytes in interface WebSocketClient
        Parameters:
        stream - the stream
        Returns:
        sink
      • receive

        public TStream<com.google.gson.JsonObject> receive()
        Create a stream of JsonObject tuples from received JSON WebSocket text messages.
        Specified by:
        receive in interface WebSocketClient
        Returns:
        the stream
      • receiveString

        public TStream<java.lang.String> receiveString()
        Create a stream of String tuples from received WebSocket text messages.

        Note, the WebSocket protocol differentiates between text/String and binary/byte messages. This method only receives messages sent as text.

        Specified by:
        receiveString in interface WebSocketClient
        Returns:
        the stream
      • receiveBytes

        public TStream<byte[]> receiveBytes()
        Create a stream of byte[] tuples from received WebSocket binary messages.

        Note, the WebSocket protocol differentiates between text/String and binary/byte messages. This method only receives messages sent as bytes.

        Specified by:
        receiveBytes in interface WebSocketClient
        Returns:
        the stream
      • topology

        public Topology topology()
        Description copied from interface: TopologyElement
        Topology this element is contained in.
        Specified by:
        topology in interface TopologyElement
        Returns:
        Topology this element is contained in.

Copyright IBM 2015,2016 - 2f6ad0e-20160307-0902