发表于: 2017-05-24 23:23:29
1 1118
今天完成的事:
1、最终还是参看师兄们的日报完成web端随机调用service的任务要求
师兄日报链接:http://www.jnshu.com/daily/19430?dailyType=others&total=92&page=39&tid=55&oid=5&sort=0&orderBy=3
@RequestMapping("/showList")
public String showList(Model model) {
log.info("showList方法被调用");
List<Student> studentList = new ArrayList<Student>();
// studentList = studentService.getAllStudent();
int flag=Math.random() > 0.5 ? 1 :0;
try {
switch (flag){
case 1:
ClassPathXmlApplicationContext context11 = new ClassPathXmlApplicationContext("spring-context01.xml");
StudentService studentService11= (StudentService) context11.getBean("studentService");
System.out.println("第一层调用service1");
studentList=studentService11.getAllStudent();
break;
default:
ClassPathXmlApplicationContext context12 = new ClassPathXmlApplicationContext("spring-context02.xml");
StudentService studentService12= (StudentService) context12.getBean("studentService01");
System.out.println("第一层调用service2");
studentList=studentService12.getAllStudent();
break;
}
}catch (Exception e){
switch (flag){
case 1:
ClassPathXmlApplicationContext context21 = new ClassPathXmlApplicationContext("spring-context02.xml");
StudentService studentService21= (StudentService) context21.getBean("studentService01");
System.out.println("第一层调用service1失败改调用service2");
studentList=studentService21.getAllStudent();
break;
default:
ClassPathXmlApplicationContext context22 = new ClassPathXmlApplicationContext("spring-context01.xml");
StudentService studentService22= (StudentService) context22.getBean("studentService");
System.out.println("第一层调用service2失败改调用service1");
studentList=studentService22.getAllStudent();
break;
}
}
model.addAttribute("studentList", studentList);
return "showlist";
}
明天计划的事:开始任务九
遇到的问题:刚开始按照自己的思路写了一个类用来封装调度不同的service的方法(返回一个连接好的service对象),本地用main方法测试没有问题(挂掉一台service 点击main方法能正确调用另外一台service)后用Tomcat运行,手动挂掉一台service程序500错误不能调换连接另外一台。思考了下能够明白问题的所在,本地每次用main方法执行时相当于重新加载了配置文件获取service的interface的bean而用Tomcat加载时只是加载一次配置文件,想了很久没想出办法来,知道在控制器里service通过手动加载配置文件链接到远程rmi,但是又认为每个service的操作都这样加载配置手动获取是不科学的。后面去官网看了一遍所有师兄任务8的日报,最终发现这样做才是较为合理的完成的任务的要求(有的师兄,只是做到将service挂掉后,再启动能连上,却没有做service挂掉后能自动切换另外一台service)。比较佩服能自己想出这种方法来实现的师兄,Java语法基础很是了得。
收获:最终是参照师兄的做出来的,感觉不到自己的收获
总结:昨天睡的比较晚,今天眼睛难受了一天,思想不集中头晕晕的,思绪比较乱,自己写个随机访问service方法的类整了一天。最后还没排上用场完成任务要求。对于任务8如果重来一次,我希望我自己能写出那样的代码,完成随机访问的逻辑。
评论