java - RestFul web service which reads properties file -


i have got couple of jersey rest web services sendpassword , resetpassword purpose send email .

for sending email , have configured properties file under tomcat , works fine

the code of sendpassword.java way

 @path("/sendpassword")     public class sendpassword {         @get         @produces("application/json")         public string sendpasswordtoemail(@queryparam("empid") string empid)                 throws jsonexception         {             try {              sendemailutility.sendmail("weqw","2312");          }         catch(exception e)         {         } } 

sendemailutility.java

public class sendemailutility      {     public static string sendmail(string sendemalto,string generatedpwd) throws ioexception {         string result = "fail";         file configdir = new file(system.getproperty("catalina.base"), "conf");         file configfile = new file(configdir, "email.properties");         inputstream stream = new fileinputstream(configfile);         properties props_load = new properties();         props_load.load(stream);         final string username = props_load.getproperty("username");         final string password = props_load.getproperty("password");         properties props_send = new properties();         props_send.put("mail.smtp.auth","true");         props_send.put("mail.smtp.starttls.enable","true");                         transport.send(message);           // code send email             result = "success";         } catch (messagingexception e) {             result = "fail";             e.printstacktrace();         }         return result;     }  } 

as can see reading properties file every call of websercice

(as reading operation costly) , there way resolve ??

could please let me know whats best approach handle this.

thanks in advance .

there few ways 1 way of doing make props_load private static member of class , call this

    public class sendemailutility      {        private static properties props;        private static properties getproperties() {        if (props == null) {         file configdir = new file(system.getproperty("catalina.base"), "conf");         file configfile = new file(configdir, "email.properties");         inputstream stream = new fileinputstream(configfile);         props = new properties();         props.load(stream);     }     return props;      }     public static string sendmail(string sendemalto,string generatedpwd) throws  ioexception {         string result = "fail";         properties props_load = getproperties();         final string username = props_load.getproperty("username");         final string password = props_load.getproperty("password");         properties props_send = new properties();         props_send.put("mail.smtp.auth","true");         props_send.put("mail.smtp.starttls.enable","true");                 transport.send(message);           // code send email             result = "success";         } catch (messagingexception e) {             result = "fail";             e.printstacktrace();         }         return result;     }  } 

the other design suggest make email service class emailsender or emailservice , inject sendpassword class

@path("/sendpassword")     public class sendpassword {         @autowired         emailservice emailservice;         @get         @produces("application/json")         public string sendpasswordtoemail(@queryparam("empid") string empid)                 throws jsonexception         { 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -