在實際生產開發中,我們經常會遇到業務邏輯的嵌套,比如說在支付場景中:
1.用戶在我們商城購買商品以後,進行支付操作
2.用戶支付完以後,我給他發一條短信告訴買家他支付成功了
3.我告訴賣家讓他發貨,因爲買家支付成功了
在這個場景中,我可以這樣寫代碼,通過最初級的方式:
@Service
@Transactional
public class PayService {
@Resource
private Msg msgService;
@Resource
private Send sendService;
@Resource
private TFlightMapper tFlightMapper;
public void pay(int user, String fcode) {
TFlight flight = new TFlight();
flight.setFcode(fcode);
flight.setOrdertime(new Date());
flight.setUser(user);
int insert = tFlightMapper.insert(flight);
if (insert > 0) {
System.out.println("用戶支付成功了~");
TMsg msg = new TMsg();
msg.setContent("訂單號爲:" + fcode);
msg.setCreatetime(new Date());
msg.setPhone("15577882500");
msg.setUser(user);
msgService.msg(msg);
Email email = new Email("系統", String.valueOf(user), "訂單支付已成功", "請您儘快發貨:" + fcode);
sendService.sendMail(email);
}
}
}
@Service
@Transactional
public class Msg {
@Resource
private TMsgMapper tMsgMapper;
public void msg(TMsg msg) {
int insert = tMsgMapper.insert(msg);
if (insert > 0) {
System.out.println("給買家發一條短信提醒!!!");
}
}
}
@Service
@Transactional
public class Send {
public void sendMail(Email email) {
try {
FileCopyUtils.copy(SerializationUtils.serialize(email), new File("e:/temp/" + email.getTitle() + ".txt"));
} catch (IOException e) {
throw new RuntimeException("io異常,異常信息爲:" + e.getMessage());
}
}
}
@Service
@Transactional
public class PayService implements ApplicationContextAware {
//ApplicationContextAware這個接口很特殊,實現這個接口就一定要重寫
//setApplicationContext這個方法,然後spring就會自動的把applicationContext
//這個對象注入到這個方法的入參,別告訴我你不知道applicationContext是啥
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Resource
private TFlightMapper tFlightMapper;
public void pay(int user, String fcode) {
System.out.println("用戶支付開始~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
TFlight flight = new TFlight();
flight.setFcode(fcode);
flight.setOrdertime(new Date());
flight.setUser(user);
int insert = tFlightMapper.insert(flight);
applicationContext.publishEvent(new PayEvent(this, fcode, user));
System.out.println("用戶支付結束~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
public class PayEvent extends ApplicationEvent {
private String fcode;
private int user;
public String getFcode() {
return fcode;
}
/*
public void setFcode(String fcode) {
this.fcode = fcode;
}
*/
public int getUser() {
return user;
}
/* public void setUser(int user) {
this.user = user;
}*/
public PayEvent(Object source, String fcode, int user) {
super(source);
this.fcode = fcode;
this.user = user;
}
}
@Service
public class MsgHandler implements ApplicationListener {
//PayEvent是這個監聽器監聽的事件
@Resource
private Msg msgService;
@Override
public void onApplicationEvent(PayEvent payEvent) {
//監聽到事件後做如下操作
System.out.println("開始發消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
TMsg msg = new TMsg();
msg.setContent("訂單號爲:" + payEvent.getFcode());
msg.setCreatetime(new Date());
msg.setPhone("12222220000");
msg.setUser(payEvent.getUser());
msgService.msg(msg);
System.out.println("消息發送完成~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
@Service
public class MailHandler implements ApplicationListener {
@Resource
private Send sendService;
@Override
public void onApplicationEvent(PayEvent payEvent) {
System.out.println("開始發郵件~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Email email = new Email("系統", String.valueOf(payEvent.getUser()), "訂單支付已成功", "請您儘快發貨:" + payEvent.getFcode());
sendService.sendMail(email);
System.out.println("郵件發送成功~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
開始了,開始了,拋異常了,沒結束。
開始了,開始了,拋異常了,沒結束。
開始了,開始了,拋異常了,沒結束。
SimpleApplicationEventMulticaster就這個類。
加上以後再運行看到了想要的效果,好了不早了,先說到這裏吧
最後溫馨提示一下,測試的時候,讓主線程sleep一會兒,別測試線程沒結束呢主線程先完了。