JAVA_RABBITMQ Learning Notes

Source: Internet
Author: User
Tags rabbitmq

First on the official web download related software: Rabbitmq-server-3.7.4.exe + Erlang for Windows (Windows environment), and then installed on the OK

To install page management features:

CMD to under RABBITMQ directory: D:\Program files\rabbitmq server\rabbitmq_server-3.7.4\sbin, input rabbitmq-plugins.bat enable Rabbitmq_management, run, and then through access: http://127.0.0.1:15672/will be able to see the relevant page, account password is: Guest, guest

Effect Chart:



First, the basic operation:

Sent by:

Package com.example.java.t0;

Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import com.rabbitmq.client.ConnectionFactory;

public class P {

    private final static String queue_name = "Hello";

    public static void Main (string[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory ();
        Factory.sethost ("localhost");

        Factory.setvirtualhost ("/ywj");
        Factory.setusername ("Guest");
        Factory.setpassword ("Guest");

        Connection Connection = Factory.newconnection ();
        Channel Channel = Connection.createchannel ();
        Channel.queuedeclare (Queue_name, False, False, false, NULL);

        String message = "Hello world!";
        Channel.basicpublish ("", queue_name, NULL, message.getbytes ());
        SYSTEM.OUT.PRINTLN ("send" "+ Message +" "");
        Channel.close ();
        Connection.close ();
    }

Receiver:

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException;

    public class C {private final static String queue_name = "Hello";
        public static void Main (string[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("Guest");

        Factory.setpassword ("Guest");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.queuedeclare (Queue_name, False, False, false, NULL); Consumer Consumer = new Defaultconsumer (channel) {@Override public void handledelivery (String con Sumertag, Envelope Envelope, AMQP. Basicproperties properties, byte[] throws IOException {String message = new STR
                ING (body, "UTF-8"); System.out.println ("received" "+ Message +" "");
            Mark 1:channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
        Channel.basicconsume (Queue_name, true, consumer); /*note:channel.basicconsume (Queue_name, true, consumer); The second argument in is true, meaning that when the recipient receives the message, the receiver automatically gives the server feedback, and the server deletes the relevant message if the comment [tag 1] Remove, means to manually give feedback to the server, at this time Channel.basicconsume (Queue_name, true, consumer), the second parameter in is not true, but false */}}

Second, work mode

The sender is the same, except that the message is sent by more:

The sender loops through multiple messages to the queue: for
(int i = 0; I <100; i++) {
    String messages = "Hello world!---" +i;
    Channel.basicpublish ("", queue_name, NULL, message.getbytes ());
    SYSTEM.OUT.PRINTLN ("send" "+ Message +" "");
}


Recipients (multiple):

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException;

    public class C {private final static String queue_name = "Hello";
        public static void Main (string[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("Guest");

        Factory.setpassword ("Guest");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.queuedeclare (Queue_name, False, False, false, NULL); Channel.basicqos (1);//Accept only one unack-ed in a time (= below) can only get a single data at the same moment Consumer Consumer = NE W Defaultconsumer (channel) {@Override public void Handledelivery (String consumertag, Envelope env Elope, AMQP. Basicproperties properties, byte[] body) throws Ioexception {String message = new String (Body, "UTF-8");
                Work (message);
            Channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
    Channel.basicconsume (Queue_name, false, consumer);
            public static void Work (String msg) {try {System.out.println ("C Receive:" +msg);
        Thread.Sleep (1000);
        catch (Interruptedexception e) {e.printstacktrace (); }
    }
}

Third, publish and subscribe

(Note: Publish and subscribe to run the receiver before running the sender, because there is a switch in this mode, the switch does not save the function of the message, so first run the receiver to declare a queue and bind the switch, the sender sent messages, the switch can forward messages to the queue)

Sent by:

Package com.example.java.t0;

Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import com.rabbitmq.client.ConnectionFactory;

public class P {

    private final static String Exchange_name = "exchange_0";//switch name public

    static void main (string[ ] args throws Exception {
        ConnectionFactory factory = new ConnectionFactory ();
        Factory.sethost ("localhost");

        Factory.setvirtualhost ("/ywj");
        Factory.setusername ("Ywj");
        Factory.setpassword ("0");

        Connection Connection = Factory.newconnection ();
        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (Exchange_name, "fanout");/bind switch


        String message = "Hello world!";
        Channel.basicpublish (Exchange_name, "", NULL, Message.getbytes ());
        SYSTEM.OUT.PRINTLN ("send" "+ Message +" "");
        Channel.close ();
        Connection.close ();
    }

Receiver 1:

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException; public class C {private final static String Exchange_name = "exchange_0";//switch name public static void main (Strin
        G[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (Exchange_name, "fanout");
        String queuename = Channel.queuedeclare (). Getqueue ();
            Channel.queuebind (QueueName, Exchange_name, "")/bound switch Consumer Consumer = new Defaultconsumer (channel) {
                                       @Override public void Handledelivery (String consumertag, Envelope Envelope, Amqp.
     Basicproperties properties, byte[] body)               Throws IOException {String message = new String (Body, "UTF-8");
                SYSTEM.OUT.PRINTLN ("C Receive:" +message);
            Channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
    Channel.basicconsume (QueueName, false, consumer); }

}

Recipient 2:

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException; public class C2 {private final static String Exchange_name = "exchange_0";//switch name public static void main (STR
        Ing[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (Exchange_name, "fanout");
        String queuename = Channel.queuedeclare (). Getqueue ();
            Channel.queuebind (QueueName, Exchange_name, "")/bound switch Consumer Consumer = new Defaultconsumer (channel) {
                                       @Override public void Handledelivery (String consumertag, Envelope Envelope, Amqp.
   Basicproperties properties, byte[] body)                 Throws IOException {String message = new String (Body, "UTF-8");
                System.out.println ("C2 receive:" +message);
            Channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
    Channel.basicconsume (QueueName, false, consumer); }
}

Four, routing mode (direct: matching the custom keyword to send)

Sent by:

Package com.example.java.t0;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;

Import Com.rabbitmq.client.ConnectionFactory; public class P {private final static string exchange_name = "Exchange_0";//switch name private final static string DI  Rect_name = "red";//routing rule public static void main (string[] args) throws Exception {ConnectionFactory Factory
        = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();
        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (Exchange_name, "direct");/bind switch String message = "Here is Information";
        Channel.basicpublish (exchange_name, direct_name, NULL, message.getbytes ());
        SYSTEM.OUT.PRINTLN ("send" "+ Message +" "");
        Channel.close ();
    Connection.close (); }
}

Recipient 1: (only receive messages from Sender direct_name=red)

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException; public class C {private final static string exchange_name = "Exchange_0";//switch name private final static string D  Irect_name = "red";//Routing rule, receiving only red public static void main (string[] args) throws Exception {ConnectionFactory
        Factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (Exchange_name, "direct");
        String queuename = Channel.queuedeclare (). Getqueue (); Channel.queuebind (QueueName, Exchange_name, direct_name)//bind switch and specify receive type Consumer Consumer = new Defaultconsumer (channel)
      {@Override public void Handledelivery (String consumertag, Envelope Envelope,                                 Amqp. Basicproperties properties, byte[] throws IOException {String message = new STR
                ING (body, "UTF-8");
                SYSTEM.OUT.PRINTLN ("C Receive:" +message);
            Channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
    Channel.basicconsume (QueueName, false, consumer); }

}

Recipient 2: (only receive messages from Sender Direct_name=blue)

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException; public class C2 {private final static string exchange_name = "Exchange_0";//switch name private final static string Direct_name = "Blue";//Routing rules, only receive Blue's public static void Main (string[] args) throws Exception {Connectionfa
        Ctory factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (Exchange_name, "direct");
        String queuename = Channel.queuedeclare (). Getqueue (); Channel.queuebind (QueueName, Exchange_name, direct_name)//bind switch and specify receive type Consumer Consumer = new Defaultconsumer (channel)
 {@Override public void Handledelivery (String consumertag, Envelope Envelope,                                      Amqp. Basicproperties properties, byte[] throws IOException {String message = new STR
                ING (body, "UTF-8");
                System.out.println ("C2 receive:" +message);
            Channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
    Channel.basicconsume (QueueName, false, consumer); }
}
V. Theme patterns (similar to regular expressions)
Way: # represents one or more words (is a word, not a single letter)

* Code A Word (is a word, not a single letter)

Sent by:

Package com.example.java.t0;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;

Import Com.rabbitmq.client.ConnectionFactory; public class P {private final static String Exchange_name = "exchange_topic";//switch name private final static Strin G topic_name = "Apple.iphone8";//when Topic_name=apple.iphone8, receiver C and receiver C2 can receive, when TOPIC_NAME=APPLE.IPHONE8.FXX, Only recipient C2 can receive public static void main (string[] args) throws Exception {ConnectionFactory factory = new Connect
        Ionfactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();
        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (exchange_name, "topic");/bind switch String message = "I am the message I am the message I am the message I am the message";
        Channel.basicpublish (exchange_name, topic_name, NULL, message.getbytes ()); System.out.priNTLN ("send" "+ Message +" "");
        Channel.close ();
    Connection.close (); }
}

Recipient C:

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException; public class C {private final static String Exchange_name = "exchange_topic";//switch name private final static Stri

    ng topic_name = "Apple.iphone8";
        public static void Main (string[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (exchange_name, "topic");
        String queuename = Channel.queuedeclare (). Getqueue ();


        Channel.queuebind (QueueName, Exchange_name, topic_name); Consumer Consumer = new Defaultconsumer (channel) {@Override public void handledelivery (String con
                        Sumertag, Envelope Envelope,               Amqp. Basicproperties properties, byte[] throws IOException {String message = new STR
                ING (body, "UTF-8");
                SYSTEM.OUT.PRINTLN ("C Receive:" +message);
            Channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
    Channel.basicconsume (QueueName, false, consumer); }

}

Recipient C2:

Package com.example.java.t0;

Import com.rabbitmq.client.*;

Import java.io.IOException; public class C2 {private final static String Exchange_name = "exchange_topic";//switch name private final static STR

    ing topic_name = "apple.#";
        public static void Main (string[] args) throws Exception {ConnectionFactory factory = new ConnectionFactory ();

        Factory.sethost ("localhost");
        Factory.setvirtualhost ("/YWJ");
        Factory.setusername ("YWJ");

        Factory.setpassword ("0");
        Connection Connection = Factory.newconnection ();

        Channel Channel = Connection.createchannel ();
        Channel.exchangedeclare (exchange_name, "topic");
        String queuename = Channel.queuedeclare (). Getqueue ();


        Channel.queuebind (QueueName, Exchange_name, topic_name); Consumer Consumer = new Defaultconsumer (channel) {@Override public void handledelivery (String con
                             Sumertag, Envelope Envelope,          Amqp. Basicproperties properties, byte[] throws IOException {String message = new STR
                ING (body, "UTF-8");
                System.out.println ("C2 receive:" +message);
            Channel.basicack (Envelope.getdeliverytag (), false);
        }
        };
    Channel.basicconsume (QueueName, false, consumer);
 }
}

Long time:

Sent by:


The switch holds long: Channel.exchangedeclare (xxx,xx,xxx ...) Method has a parameter Boolean durable, set to True
Long queue: Channel.queuedeclare (xxx,xx,xxx ...) Method has a parameter Boolean durable, set to True
The message is prolonged: Channel.basicpublish ("", Queue_name, Messageproperties.persistent_text_plain,message.getbytes ()); The core is messageproperties.persistent_text_plain.

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.