Java uses NANOMSG message middleware

Source: Internet
Author: User

One, the referenced package

<dependency>    <groupId>jnanomsg</groupId>    <artifactId>jnanomsg</artifactId>    <version>0.4.3</version></dependency>

Second, the use of nanomsg, the need for different platforms to compile and get a dynamic library, and then introduce the project. At present, we have compiled two dynamic libraries in window10-x86-64 and linux-x86-64 environments. (This time use is nanomsg5.1.0)

Windows: Placing Nanomsg.dll and nanomsg.lib directly under the Classpath
Linux: Create a directory linux-x86-64 under the Classpath and place the libnanomsg.so file under it

Download path: Https://pan.baidu.com/s/18-wE_QMeYDkwi4kYQfrAaw

Third, borrow someone else's code
1,pair mode

public class Pair {private static String URL = "tcp://127.0.0.1:7789";        public static void Main (string[] args) {node0 ();    Node1 ();        } private static void Node0 () {pairsocket socket = new Pairsocket ();        Socket.connect (URL);        Send (socket);    RECV (socket, "node0");        } private static void Node1 () {pairsocket socket = new Pairsocket ();        Socket.bind (URL);        Send (socket);    RECV (socket, "Node1");   } private static void recv (final pairsocket socket, final String nodeName) {socket.setrecvtimeout (2000);                    Set timeout for execution recv new Thread (new Runnable () {public void run () {while (true) {  try {System.out.println (nodeName + ":" + socket.recvstring ());                    Blocks the socket until it expires or responds with a thread.sleep (1000);                    } catch (IOException e) {//Ignore timeout e.printstacktrace ();} catch (Interruptedexception e) {e.printstacktrace ();    }}}). Start ();        } private static void Send (final pairsocket socket) {socket.setsendtimeout (1100);                    Set timeout for executing send new Thread (new Runnable () {public void run () {while (true) {                        try {socket.send ("Hello");                    Thread.Sleep (1000);                    } catch (Interruptedexception e) {e.printstacktrace ();                    } catch (IOException e) {e.printstacktrace ();    }}}). Start (); }}

2,pipeline mode

  public class Pipeline {private static String URL = "tcp://127.0.0.1:7789";        public static void Main (string[] args) {node1 ();    Node0 ();        } private static void Node1 () {final Pullsocket socket = new Pullsocket ();        Socket.bind (URL);                        New Thread (New Runnable () {public void run () {while (true) {try {  System.out.println (Socket.recvstring ());                    Blocks the socket until it expires or responds with a thread.sleep (1000);                    } catch (IOException e) {//Ignore timeout//E.printstacktrace ();                    } catch (Interruptedexception e) {//E.printstacktrace ();    }}}). Start ();        } private static void Node0 () {final Pushsocket socket = new Pushsocket ();        Socket.connect (URL);    Socket.send ("Hello"); }}

3,reqrep mode

public class Reqrep {private static String URL = "tcp://127.0.0.1:7789";        public static void Main (string[] args) {node1 ();    Node0 ();        } private static void Node1 () {final Repsocket socket = new Repsocket ();        Socket.bind (URL);                        New Thread (New Runnable () {public void run () {while (true) {try {  System.out.println ("Node1:" + socket.recvstring ());                        Blocks the socket until it expires or responds with a thread.sleep (1000);                    Socket.send ("World");                    } catch (IOException e) {//Ignore timeout e.printstacktrace ();                    } catch (Interruptedexception e) {e.printstacktrace ();    }}}). Start ();        } private static void Node0 () {final Reqsocket socket = new Reqsocket ();        Socket.connect (URL);       New Thread (New Runnable () {     public void Run () {while (true) {try {socket.send ("Hello"                        );                        Thread.Sleep (1000);  System.out.println ("Node0:" + socket.recvstring ());                    Blocks the socket until it expires or has a response} catch (Interruptedexception e) {e.printstacktrace ();                    } catch (IOException e) {//E.printstacktrace ();    }}}). Start (); }}

4,pubsub mode

public class PubSub {private static String URL = "tcp://127.0.0.1:7789";        public static void Main (string[] args) {service ();        Client0 ();        Client1 ();    Client2 ();    } private static void Client0 () {Client ("client0");    } private static void Client1 () {Client ("client1");    } private static void Client2 () {Client ("Client2");        } private static void client (final String name) {Final Subsocket socket = new Subsocket ();        Socket.connect (URL);   Socket.subscribe ("test"); The Jnanomsg channel match is a message that matches recv to ^test new Thread (new Runnable () {public void run () {while (true)                    {try{System.out.println (name + ":" + socket.recvstring ());                    }catch (IOException e) {//Ignore timeout//e.printstacktrace ();    }}}). Start (); } private static void service() {final Pubsocket socket = new Pubsocket ();        Socket.bind (URL);                        New Thread (New Runnable () {public void run () {while (true) {try{                        Socket.send ("Test1 msg");                    Thread.Sleep (2000);                    }catch (IOException e) {//Ignore timeout e.printstacktrace ();                    } catch (Interruptedexception e) {e.printstacktrace ();    }}}). Start (); }}

5,bus mode

public class Bus {private static String url0 = "tcp://127.0.0.1:7780";    private static String URL1 = "tcp://127.0.0.1:7781";    private static String Url2 = "tcp://127.0.0.1:7782";    private static String Url3 = "tcp://127.0.0.1:7783";        public static void Main (string[] args) {Bussocket s0 = node ("Node0", url0, New STRING[]{URL1, Url2, url3});        Bussocket S1 = node ("Node1", URL1, New String[]{url2, url3});        Bussocket s2 = node ("Node2", Url2, New String[]{url3});        Bussocket s3 = node ("Node3", Url3, New string[]{});        S0.send ("Client0 send a");        S1.send ("Client1 send a");        S2.send ("Client2 send a");    S3.send ("Client3 send a"); } private static Bussocket node (final string name, String self, string[) {final Bussocket socket = new B        Ussocket ();        Socket.bind (self);        for (String s:other) {socket.connect (s);              } New Thread (new Runnable () {public void run () {  while (true) {try {System.out.println (name + ":" + socket.recvstring ());                    Blocks the socket until it expires or responds with a thread.sleep (1);                    } catch (IOException e) {//Ignore timeout//E.printstacktrace ();                    } catch (Interruptedexception e) {//E.printstacktrace ();        }}}). Start ();        return socket;    Socket.connect (); }}

Java uses nanomsg message middleware

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.