A simple Java program for RABBITMQ log monitoring

Source: Internet
Author: User
Tags rabbitmq amq

All logs for RABBITMQ will be sent to the topic type of Exchange "Amq.rabbitmq.log" Routingkey debug,info,waring,error. If you receive all types of logs, you can use #

 Packagelogs;Importjava.io.IOException;Importjava.util.concurrent.TimeoutException;ImportCom.rabbitmq.client.Channel;Importcom.rabbitmq.client.Connection;ImportCom.rabbitmq.client.DefaultConsumer;ImportCom.rabbitmq.client.Envelope;Importcom.rabbitmq.client.AMQP.BasicProperties;ImportUtils. Channelutils; Public classListenerrabbitmqlogs {Private Static FinalString queue_name_debug = "Queue_debug"; Private Static FinalString queue_name_info = "Queue_info"; Private Static FinalString queue_name_warning = "Queue_warning"; Private Static FinalString queue_name_error = "Queue_error"; Private Static FinalString Exchange_name_log = "Amq.rabbitmq.log";  Public Static voidMain (string[] args)throwsIOException, timeoutexception {Connection Connection= Channelutils.getconnection ("Listenerlog"); Channel Channeldebug=Connection.createchannel (); Channel Channelinfo=Connection.createchannel (); Channel channelwarning=Connection.createchannel (); Channel Channelerror=Connection.createchannel ();        Channeldebug.queuedelete (Queue_name_debug); Channeldebug.queuedeclare (Queue_name_debug,false,false,false,NULL);        Channelinfo.queuedelete (Queue_name_info); Channelinfo.queuedeclare (Queue_name_info,false,false,false,NULL);        Channelwarning.queuedelete (queue_name_warning); Channelwarning.queuedeclare (queue_name_warning,false,false,false,NULL);        Channelerror.queuedelete (Queue_name_error); Channelerror.queuedeclare (Queue_name_error,false,false,false,NULL); Channeldebug.queuebind (Queue_name_debug, Exchange_name_log,"Debug"); Channelinfo.queuebind (Queue_name_info, Exchange_name_log,"Info"); Channelwarning.queuebind (queue_name_warning, Exchange_name_log,"Warning"); Channelerror.queuebind (Queue_name_error, Exchange_name_log,"Error"); Channeldebug.basicconsume (Queue_name_debug,NewLogsconsumer (Channeldebug, "Debug")); Channelinfo.basicconsume (Queue_name_info,NewLogsconsumer (Channelinfo, "Info")); Channelwarning.basicconsume (queue_name_warning,NewLogsconsumer (channelwarning, "Warning")); Channelerror.basicconsume (Queue_name_error,NewLogsconsumer (Channelerror, "Error")); }}classLogsconsumerextendsDefaultconsumer {PrivateString LogLevel;  PublicLogsconsumer (Channel channel,string logLevel) {Super(channel);  This. LogLevel =LogLevel; } @Override Public voidHandledelivery (String Consumertag, Envelope Envelope, basicproperties properties,byte[] body)throwsIOException {System.out.println (logLevel+":"+NewString (body)); //Use the manual confirmation mode, where you need to acknowledge receipt of the message. Getchannel (). Basicack (Envelope.getdeliverytag (),false); }}
 PackageUtils;Importjava.io.IOException;Importjava.util.concurrent.TimeoutException;ImportCom.rabbitmq.client.Channel;Importcom.rabbitmq.client.Connection;Importcom.rabbitmq.client.ConnectionFactory; Public classChannelutils {     Public StaticChannel getchannelinstance (String connectiondescription) {Try {            returngetconnection (connectiondescription). CreateChannel (); } Catch(Exception e) {Throw NewRuntimeException ("Get channel Connection Failed"); }    }     Public StaticConnection getconnection (String connectiondescription)throwsIOException, timeoutexception {connectionfactory connectionfactory=getconnectionfactory (); returnconnectionfactory.newconnection (connectiondescription); }     Public Staticconnectionfactory getconnectionfactory () {connectionfactory connectionfactory=NewConnectionFactory (); Connectionfactory.sethost ("192.168.1.111"); Connectionfactory.setport (5672); Connectionfactory.setvirtualhost ("/"); Connectionfactory.setusername ("DRS"); Connectionfactory.setpassword ("123456");

returnConnectionFactory; }}

A simple Java program for RABBITMQ log monitoring

Related Article

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.