@PostMapping("/annotation")
public Customer createInAnnotation(@RequestBody Customer customer) {
return customerService.create(customer);
}
@PostMapping("/code")
public Customer createInCode(@RequestBody Customer customer) {
return customerServiceInCode.create(customer);
}
@Transactional
@PostMapping("/message/annotation")
public void createMsgWithListener(@RequestParam String userName) {
jmsTemplate.convertAndSend("customer:msg:new", userName);
}
@Transactional
@PostMapping("/message/code")
public void createMsgDirect(@RequestParam String userName) {
jmsTemplate.convertAndSend("customer:msg2:new", userName);
}
注解
@Transactional
public Customer create(Customer customer) {
LOG.info("CustomerService In Annotation create customer:{}", customer.getUsername());
if (customer.getId() != null) {
throw new RuntimeException("用户已经存在");
}
customer.setUsername("Annotation:" + customer.getUsername());
jmsTemplate.convertAndSend("customer:msg:reply", customer.getUsername() + " created.");
return customerRepository.save(customer);
}
@Transactional
@JmsListener(destination = "customer:msg:new")
public Customer createByListener(String name) {
LOG.info("CustomerService In Annotation by Listener create customer:{}", name);
Customer customer = new Customer();
customer.setUsername("Annotation:" + name);
customer.setRole("USER");
customer.setPassword("111111");
jmsTemplate.convertAndSend("customer:msg:reply", customer.getUsername() + " created.");
return customerRepository.save(customer);
}
代码
public Customer create(Customer customer) {
LOG.info("CustomerService In Code create customer:{}", customer.getUsername());
if (customer.getId() != null) {
throw new RuntimeException("用户已经存在");
}
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
def.setTimeout(15);
TransactionStatus status = transactionManager.getTransaction(def);
try {
customer.setUsername("Code:" + customer.getUsername());
customerRepository.save(customer);
jmsTemplate.convertAndSend("customer:msg:reply", customer.getUsername() + " created.");
transactionManager.commit(status);
return customer;
} catch (Exception e) {
transactionManager.rollback(status);
throw e;
}
}
@JmsListener(destination = "customer:msg2:new")
public void createByListener(String name) {
LOG.info("CustomerService In Code by Listener create customer:{}", name);
Customer customer = new Customer();
customer.setUsername("Code:" + name);
customer.setRole("USER");
customer.setPassword("111111");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
def.setTimeout(15);
TransactionStatus status = transactionManager.getTransaction(def);
try {
customerRepository.save(customer);
jmsTemplate.convertAndSend("customer:msg:reply", customer.getUsername() + " created.");
transactionManager.commit(status);
} catch (Exception e) {
transactionManager.rollback(status);
throw e;
}
}
网友评论