Package org.zeromq

Class ZSocket

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ZSocket
    extends java.lang.Object
    implements java.lang.AutoCloseable
    ZeroMQ sockets present an abstraction of an asynchronous message queue, with the exact queuing semantics depending on the socket type in use. Where conventional sockets transfer streams of bytes or discrete datagrams, ZeroMQ sockets transfer discrete messages.

    ZeroMQ sockets being asynchronous means that the timings of the physical connection setup and tear down, reconnect and effective delivery are transparent to the user and organized by ZeroMQ itself. Further, messages may be queued in the event that a peer is unavailable to receive them.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.atomic.AtomicBoolean isClosed  
      private SocketBase socketBase  
    • Constructor Summary

      Constructors 
      Constructor Description
      ZSocket​(int socketType)
      Create a ZeroMQ socket
      ZSocket​(SocketType socketType)
      Create a ZeroMQ socket
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean bind​(java.lang.String endpoint)
      Creates an endpoint for accepting connections and binds to it.
      void close()
      boolean connect​(java.lang.String endpoint)
      Connects the socket to an endpoint and then accepts incoming connections on that endpoint.
      boolean disconnect​(java.lang.String endpoint)
      Disconnecting a socket from an endpoint.
      private java.lang.Object getOption​(int option)  
      SocketType getSocketType()
      Retrieve the socket type for the current 'socket'.
      int getType()
      Retrieve the socket type for the current 'socket'.
      boolean hasReceiveMore()
      Returns a boolean value indicating if the multipart message currently being read from the Socket and has more message parts to follow.
      private void mayRaise()  
      byte[] receive()  
      byte[] receive​(int flags)  
      java.lang.String receiveStringUtf8()  
      java.lang.String receiveStringUtf8​(int flags)  
      int send​(byte[] b)  
      int send​(byte[] b, int flags)  
      boolean sendFrame​(ZFrame frame, int flags)
      Send a frame
      boolean sendMessage​(ZMsg message)  
      int sendStringUtf8​(java.lang.String str)  
      int sendStringUtf8​(java.lang.String str, int flags)  
      private void setOption​(int option, java.lang.Object value)  
      void subscribe​(byte[] topic)  
      void subscribe​(java.lang.String topic)  
      boolean unbind​(java.lang.String endpoint)
      Stop accepting connections on a socket.
      void unsubscribe​(byte[] topic)  
      void unsubscribe​(java.lang.String topic)  
      • Methods inherited from class java.lang.Object

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

      • isClosed

        private final java.util.concurrent.atomic.AtomicBoolean isClosed
    • Constructor Detail

      • ZSocket

        public ZSocket​(int socketType)
        Create a ZeroMQ socket
        Parameters:
        socketType - ZeroMQ socket type
      • ZSocket

        public ZSocket​(SocketType socketType)
        Create a ZeroMQ socket
        Parameters:
        socketType - ZeroMQ Socket type
    • Method Detail

      • getSocketType

        public SocketType getSocketType()
        Retrieve the socket type for the current 'socket'. The socket type is specified at socket creation time and cannot be modified afterwards.
        Returns:
        the socket's type.
      • getType

        public int getType()
        Retrieve the socket type for the current 'socket'. The socket type is specified at socket creation time and cannot be modified afterwards.
        Returns:
        the socket's type.
        See Also:
        getSocketType()
      • bind

        public boolean bind​(java.lang.String endpoint)
        Creates an endpoint for accepting connections and binds to it.

        The endpoint argument is a string consisting of two parts as follows: transport ://address. The transport part specifies the underlying transport protocol to use. The meaning of the address part is specific to the underlying transport protocol selected.

        Parameters:
        endpoint - the endpoint to bind to
        Returns:
        returns true if bind to the endpoint was successful
      • unbind

        public boolean unbind​(java.lang.String endpoint)
        Stop accepting connections on a socket.

        Shall unbind from the endpoint specified by the endpoint argument.

        Parameters:
        endpoint - the endpoint to unbind from
        Returns:
        returns true if unbind to the endpoint was successful
      • connect

        public boolean connect​(java.lang.String endpoint)
        Connects the socket to an endpoint and then accepts incoming connections on that endpoint.

        The endpoint is a string consisting of a transport :// followed by an address. The transport specifies the underlying protocol to use. The address specifies the transport-specific address to connect to.

        Parameters:
        endpoint - the endpoint to connect to
        Returns:
        returns true if connecting to the endpoint was successful
      • disconnect

        public boolean disconnect​(java.lang.String endpoint)
        Disconnecting a socket from an endpoint.
        Parameters:
        endpoint - the endpoint to disconnect from
        Returns:
        returns true if disconnecting to endpoint was successful
      • hasReceiveMore

        public final boolean hasReceiveMore()
        Returns a boolean value indicating if the multipart message currently being read from the Socket and has more message parts to follow. If there are no message parts to follow or if the message currently being read is not a multipart message a value of false shall be returned. Otherwise, a value of true shall be returned.
        Returns:
        true if there are more messages to receive.
      • subscribe

        public void subscribe​(byte[] topic)
      • subscribe

        public void subscribe​(java.lang.String topic)
      • unsubscribe

        public void unsubscribe​(byte[] topic)
      • unsubscribe

        public void unsubscribe​(java.lang.String topic)
      • send

        public int send​(byte[] b)
      • send

        public int send​(byte[] b,
                        int flags)
      • sendFrame

        public boolean sendFrame​(ZFrame frame,
                                 int flags)
        Send a frame
        Parameters:
        frame -
        flags -
        Returns:
        return true if successful
      • sendMessage

        public boolean sendMessage​(ZMsg message)
      • sendStringUtf8

        public int sendStringUtf8​(java.lang.String str)
      • sendStringUtf8

        public int sendStringUtf8​(java.lang.String str,
                                  int flags)
      • receive

        public byte[] receive()
      • receive

        public byte[] receive​(int flags)
      • receiveStringUtf8

        public java.lang.String receiveStringUtf8()
      • receiveStringUtf8

        public java.lang.String receiveStringUtf8​(int flags)
      • mayRaise

        private void mayRaise()
      • setOption

        private void setOption​(int option,
                               java.lang.Object value)
      • getOption

        private java.lang.Object getOption​(int option)
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable