Please comment on my Spring-Rabbit AQMP logger class -


i writing springboot 4 app , want send log messages controllers using rabbitmq 3.5.5. wrote class, using java api, encapsulate rabbit stuff, advised each client request handled in different thread. told if allocate channel each time send performance poor, should use spring rabbitmq implementation handles issue me.

i rewrote class, , i'm hoping willing @ it. although seems work nicely, i'm still new springboot, feel don't have complete understanding of how magic implemented. primary concerns class (1) thread safety , (2) performance.

currently, i'm autowiring class in controllers this:

@autowired private rabbitlogger rl; 

i think gives me new instance of rabbitlogger , if think i'm on thread safety, i'm not sure. new rabbitlogger class uses spring rabbitmq client implementation.

i'll post class , excerpt controller below. in logger class, use spring.rabbitmq application.properties username, password , host, , created own properties queue name , exchange name.

your thoughts appreciated. highly visible project @ organization , need close perfect possible. thank you.

@configuration public class rabbitlogger  {     @value("${rabbit.queue:uadec_queue}")     private string queuename;      @value("${rabbit.exchange:uadec_exchange}")     private string exchangename;      @value("${spring.rabbitmq.username}")     private string rabbitun;      @value("${spring.rabbitmq.password}")     private string rabbitpw;      @value("${spring.rabbitmq.host}")     private string rabbithost;      @autowired     private rabbittemplate rabbittemplate;      @postconstruct     private void dumpconfig()     {         logger.getglobal().config(() -> "rabbitmq hostname: " + rabbithost);         logger.getglobal().config(() -> "rabbitmq exchange name: " + exchangename);         logger.getglobal().config(() -> "rabbitmq queue name: " + queuename);         logger.getglobal().config(() -> "rabbitmq username: " + rabbitun);         logger.getglobal().config(() -> "rabbitmq password: " + rabbitpw);     }      public void logloginusernamenotfound(string ip, string s)     {         string message = ip + "\tlogin_user_not_found\t" + s;         send(message);     }      public void logloginbadpassword(string ip, string s)     {         string message = ip + "\tlogin_bad_password\t" + s;          send(message);     }       public void logloginsuccess(string ip, string s)     {         string message = ip + "\tlogin_success\t" + s;          send(message);     }      public void logdecisionrendered(string ip, string userid, string decision)     {         string message = ip + "\tdecision_rendered\t" + userid + "\t" + decision;          send(message);     }      private void send(string s)     {         if(rabbittemplate == null)         {             logger.getglobal().fine(() -> "unable send rabbit message - rabbittemplate null");             return;         }          if(queuename == null)         {             logger.getglobal().fine(() -> "unable send rabbit message - queuename null");             return;         }          string time = zoneddatetime.now().tostring();         string message = time + "\t" + s + "\r\n";          try         {             rabbittemplate.convertandsend(queuename, message);              logger.getglobal().finest(() -> "sent rabbit message: " + message);         }         catch(exception e)         {             logger.getglobal().finest(() -> "failed sending rabbit message: " + message);             logger.getglobal().finest(() -> "exception: " + e);         }     } } 

and controller starts this:

@controller public class login_formcontroller  {     private string decisiontext = null;      @autowired     private admindatarepository adrep;      @autowired     private applicantcredentialsdatarepository acrep;      @autowired     private rabbitlogger rl;      @requestmapping(value="/", method=requestmethod.post)     public string loginsumbit(@valid          @modelattribute("loginform") applicantcredentialsdata applicantcredentialsdata,         bindingresult br,         model model,         httpservletrequest serreq)     {          blah blah blah          // log         rl.logloginsuccess(remoteip, userid);      } 

not sure how can , glad see other vision question, perspective looks good.

yes, there lot of code specific project, use spring amqp correctly.

the rabbittemplate thread-safe, no need worry concurrent convertandsend invocation.

the code

@autowired private rabbitlogger rl; 

in login_formcontroller (btw it's bad class name java convernsion perspective) says use 1 instance container - singleton. , because rabbitlogger defined without scope. i'd don't worry subject, if don't understand spring properly. read documentation on matter now!

the spring boot magic around population application context (your one!) built-in beans, rabbittemplate based on common options application.properties. can find how done rabbitautoconfiguration.

what else you?..

we have amqpappender log4j , logback out-of-the-box components allow follow standard logging system config , api, send logs amqp.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -