RABBITMQ Basic Function test Case (Java implementation)

Source: Internet
Author: User
Tags rabbitmq

To test whether RABBITMQ is useful, a RABBITMQ basic functional test Case written in the Java language is written for informational purposes only.

Code Description:

Because the implementation language is Java, there are Java virtual machines (JDK or JRE installed) to test, no need to install third-party modules like Python, easy to test in the Docker environment. The Amqp-client-3.x.x.jar library is used in this test case and can be downloaded on your own.

In order to implement a Java source file to achieve the receiving and sending (compiled or 3 class files), in the main function of the two threads, one responsible for the hair, a responsible for the collection, to test the RABBITMQ message receiving and sending. The sending message is sent every 2s (implemented: Java Thread Sleep), in order to add a Java random number (implementation: Java random) to different message content each time the message content is sent.

The code is as follows:

package com.devops.broker.rabbitmq.client;import com.rabbitmq.client.channel;import  com.rabbitmq.client.connection;import com.rabbitmq.client.connectionfactory;import  com.rabbitmq.client.queueingconsumer;import java.io.ioexception;import java.util.random;class  send extends thread {    private final static string  queue_name =  "Hello";     public void run ()  {         connectionfactory factory = new connectionfactory ();         factory.sethost ("10.6.28.46");         factory.setport (5672);         factory.setusername ("Guest") ;         factory.setpassword ("Guest");         random randomintnumber  = new random ();        try {             while  (True)  {                 Connection connection =  Factory.newconnection ();                 channel channel = connection.createchannel ();                 channel.queuedeclare (QUEUE_NAME, false,  False, false, null);                 String message =  "hello world!";                 Long  Threadid = send.currentthread (). GetId ();                 message = message +   " Thread ID: "  + threadid.tostring ()  +  " Random Num: "  + randomintnumber.nextint ();                 channel.basicpublish ("",  queue_name, null, message.getbytes ());                  System.out.println ("sent "  + message +  "'");                 system.out.println ("");                 channel.close ();                 connection.close ();                 try {                     thread.sleep (;     )            } catch  (interruptedexception  e)  {                     e.printstacktrace ();                 }            }         } catch  (exception e)  {             system.out.println (E.getmessage ());         }    }}class receiver extends thread {     private final static String QUEUE_NAME =  "Hello";    public  Void run ()  {        connectionfactory factory =  new connectionfactory ();         factory.sethost ("10.6.28.46 ");         factory.setport (5672);         factory.setusername ("Guest");         factory.setpassword (" Guest ");        try {             connection connection = factory.newconnection ();             Channel channel =  Connection.createchannel ();             Channel.queuedeclare (queue_name, false, fAlse, false, null);             Queueingconsumer consumer = new queueingconsumer (channel);             channel.basicconsume (Queue_name, true, consumer);             while  (True)  {                 QueueingConsumer.Delivery  Delivery = consumer.nextdelivery ();                 string message = new string (Delivery.getBody ());                 system.out.println (" received  '  + message +  ' ");             }        } catch  (exception e)  {             system.out.println (E.getmessage ());         }    }}public class main {    public  static void main (String[] args)  throws IOException {         send send = new send ();         send.start ();        receiver receiver =  New receiver ();         receiver.start ();     } }

TAG:RABBITMQ Java Demo, test case, functional test

--end--

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1794635

RABBITMQ Basic Function test Case (Java implementation)

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.