springboot通过注解实现多线程
1、springboot启动类中添加 @enableasync
package com.zr.gktjweb; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.scheduling.annotation.enableasync; @springbootapplication @enableasync public class gktjwebapplication { public static void main(string[] args) { springapplication.run(gktjwebapplication.class, args); } }
2、在调用类之外的另一个类方法中添加
package com.zr.gktjweb.aspect; import org.springframework.scheduling.annotation.async; import org.springframework.stereotype.component; //@component public class asythread { // @override @async public void run() { system.out.println("---------异步线程---------"+thread.currentthread().getname()); } }
3、在另一个类中调用此方法
package com.zr.gktjweb.controller.login; import com.zr.gktjweb.aspect.asythread; import com.zr.gktjweb.common.httpclientutil; import com.zr.gktjweb.common.responsebean; import com.zr.gktjweb.constant.syscode; import com.zr.gktjweb.model.sysuser; import com.zr.gktjweb.util.comutil; import com.zr.gktjweb.util.jsonutils; import com.zr.gktjweb.util.userutil; import io.swagger.annotations.api; import io.swagger.annotations.apiimplicitparam; import io.swagger.annotations.apiimplicitparams; import io.swagger.annotations.apioperation; import net.sf.json.jsonobject; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.httpservletrequest; import java.util.hashmap; import java.util.map; @api(tags = "登录") @controller public class logincontroller { @value("${baseurl}") private string baseurl; @value("${login_url}") private string loginurl; @value("${logout_url}") private string logouturl; @value("${syslogin_url}") private string sysloginurl; @value("${updateusersessionbyusername_url}") private string updateusersessionbyusernameurl; @value("${updatepass_url}") private string updatepassurl; private static final logger logger = loggerfactory.getlogger(logincontroller.class); @apioperation(value = "登录", notes = "登录验证") @apiimplicitparams({@apiimplicitparam(name = "username", value = "姓名", required = true, datatype = "string"), @apiimplicitparam(name = "password", value = "密码", required = true, datatype = "string") }) @requestmapping(value = "/login.do", method = requestmethod.post) @responsebody public responsebean login(string username, string password) { string url = baseurl + loginurl; map<string, string> map = new hashmap<>(); map.put("username", username); map.put("password", password); string result = ""; new asythread().run(); system.out.println("--------当前线程--------"+thread.currentthread().getname()); //请求服务失败 try { result = httpclientutil.dopost(url, map); } catch (exception e) { e.printstacktrace(); logger.error("用户登录", e); return new responsebean(syscode.errcode, "系统异常,请联系管理员", ""); } jsonobject json = jsonobject.fromobject(result); //登录验证未通过 if (!json.getstring("code").equals(syscode.successcode + "")) { responsebean responsebean = jsonutils.jsontoobject(responsebean.class, result); return responsebean; } //验证通过,在session中设置token jsonobject datajson = (jsonobject) json.get("data"); string userjson = datajson.getstring("user"); sysuser sysuser = jsonutils.jsontoobject(sysuser.class, userjson); jsonobject tokenjson = (jsonobject) datajson.get("token"); string token = tokenjson.getstring("token"); userutil.settoken(token); userutil.setusersession(sysuser); //第一次登陆修改密码 if(comutil.isempty(sysuser.getlastlogindate())){ return new responsebean(syscode.retpwdcode, "", ""); } string url1 = "/index.html"; map<string, object> resmap = new hashmap<>(); resmap.put("url", url1); return new responsebean(syscode.successcode, "登录成功", resmap); } /** * 注销登录 * * @param request * @return */ @requestmapping("/loginout.do") public string loginout(httpservletrequest request) { request.getsession().invalidate(); string url=baseurl+logouturl; httpclientutil.doget(url); return "redirect:/"; } @postmapping("/users/updatepass.do") @apioperation(value = "修改密码") @responsebody public responsebean changepassword(string username, string oldpassword, string newpassword) { string url = baseurl + updatepassurl; map<string, string> map = new hashmap<>(); map.put("username", username); map.put("oldpassword", oldpassword); map.put("newpassword", newpassword); string result = httpclientutil.dopost(url, map); responsebean responsebean = jsonutils.jsontoobject(responsebean.class, result); //修改密码后重新登录 responsebean loginresult=login(username,newpassword); //登录验证未通过 if (!loginresult.getcode().tostring().equals(syscode.successcode + "")) { return loginresult; } return responsebean; } @apioperation(value = "当前登录用户") @getmapping("/sys/login.do") @responsebody public sysuser getlogininfo() { return userutil.getcurrentuser(); } }
调用controller层后结果如下:
实现异步失败。
5、后改为将实现异步的类
注入到controller层。通过spring生成的bean来调用异步方法:
package com.zr.gktjweb.aspect; import org.springframework.scheduling.annotation.async; import org.springframework.stereotype.component; @component public class asythread { // @override @async public void run() { system.out.println("---------异步线程---------"+thread.currentthread().getname()); } }
package com.zr.gktjweb.controller.login; import com.zr.gktjweb.aspect.asythread; import com.zr.gktjweb.common.httpclientutil; import com.zr.gktjweb.common.responsebean; import com.zr.gktjweb.constant.syscode; import com.zr.gktjweb.model.sysuser; import com.zr.gktjweb.util.comutil; import com.zr.gktjweb.util.jsonutils; import com.zr.gktjweb.util.userutil; import io.swagger.annotations.api; import io.swagger.annotations.apiimplicitparam; import io.swagger.annotations.apiimplicitparams; import io.swagger.annotations.apioperation; import net.sf.json.jsonobject; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.httpservletrequest; import java.util.hashmap; import java.util.map; @api(tags = "登录") @controller public class logincontroller { @value("${baseurl}") private string baseurl; @value("${login_url}") private string loginurl; @value("${logout_url}") private string logouturl; @value("${syslogin_url}") private string sysloginurl; @value("${updateusersessionbyusername_url}") private string updateusersessionbyusernameurl; @value("${updatepass_url}") private string updatepassurl; private static final logger logger = loggerfactory.getlogger(logincontroller.class); @autowired asythread asythread; @apioperation(value = "登录", notes = "登录验证") @apiimplicitparams({@apiimplicitparam(name = "username", value = "姓名", required = true, datatype = "string"), @apiimplicitparam(name = "password", value = "密码", required = true, datatype = "string") }) @requestmapping(value = "/login.do", method = requestmethod.post) @responsebody public responsebean login(string username, string password) { string url = baseurl + loginurl; map<string, string> map = new hashmap<>(); map.put("username", username); map.put("password", password); string result = ""; asythread.run(); system.out.println("--------当前线程--------"+thread.currentthread().getname()); //请求服务失败 try { result = httpclientutil.dopost(url, map); } catch (exception e) { e.printstacktrace(); logger.error("用户登录", e); return new responsebean(syscode.errcode, "系统异常,请联系管理员", ""); } jsonobject json = jsonobject.fromobject(result); //登录验证未通过 if (!json.getstring("code").equals(syscode.successcode + "")) { responsebean responsebean = jsonutils.jsontoobject(responsebean.class, result); return responsebean; } //验证通过,在session中设置token jsonobject datajson = (jsonobject) json.get("data"); string userjson = datajson.getstring("user"); sysuser sysuser = jsonutils.jsontoobject(sysuser.class, userjson); jsonobject tokenjson = (jsonobject) datajson.get("token"); string token = tokenjson.getstring("token"); userutil.settoken(token); userutil.setusersession(sysuser); //第一次登陆修改密码 if(comutil.isempty(sysuser.getlastlogindate())){ return new responsebean(syscode.retpwdcode, "", ""); } string url1 = "/index.html"; map<string, object> resmap = new hashmap<>(); resmap.put("url", url1); return new responsebean(syscode.successcode, "登录成功", resmap); } /** * 注销登录 * * @param request * @return */ @requestmapping("/loginout.do") public string loginout(httpservletrequest request) { request.getsession().invalidate(); string url=baseurl+logouturl; httpclientutil.doget(url); return "redirect:/"; } @postmapping("/users/updatepass.do") @apioperation(value = "修改密码") @responsebody public responsebean changepassword(string username, string oldpassword, string newpassword) { string url = baseurl + updatepassurl; map<string, string> map = new hashmap<>(); map.put("username", username); map.put("oldpassword", oldpassword); map.put("newpassword", newpassword); string result = httpclientutil.dopost(url, map); responsebean responsebean = jsonutils.jsontoobject(responsebean.class, result); //修改密码后重新登录 responsebean loginresult=login(username,newpassword); //登录验证未通过 if (!loginresult.getcode().tostring().equals(syscode.successcode + "")) { return loginresult; } return responsebean; } @apioperation(value = "当前登录用户") @getmapping("/sys/login.do") @responsebody public sysuser getlogininfo() { return userutil.getcurrentuser(); } }
访问contrller层后,结果如下:
成功实现异步。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论