WebService学习(八)——利用websevice接口封装activemq的生产者,并监听到消息

本系列第五篇,利用myeclipse生成基于jax-ws的接口,并能成功访问wsdl后,接下其他操作

1.service接口:


     
     
     
     
  1. /**
  2. * 用于外部系统调用接口
  3. *
  4. * @author Administrator
  5. *
  6. */
  7. public interface IUserService {
  8. public Users getUsersByUserCode(String userCode);
  9. public Users selectByPrimaryKey(Integer id);
  10. public int deleteByPrimaryKey(Integer id);
  11. public int insert(Users record);
  12. public int insertSelective(Users record);
  13. public int updateByPrimaryKeySelective(Users record);
  14. public int updateByPrimaryKey(Users record);
  15. }

2.UserService类代码


     
     
     
     
  1. /**
  2. * 用户信息操作服务
  3. */
  4. @Service
  5. public class UserService implements IUserService {
  6. private UsersDao usersDao;
  7. @Resource
  8. private ProducerServiceUser producerServiceUser;
  9. @Resource(name = "userQueueDestination")
  10. private Destination destination;
  11. //查询方法
  12. public Users getUsersByUserCode(String userCode) {
  13. return usersDao.getUsersByUserCode(userCode);
  14. }
  15. public Users selectByPrimaryKey(Integer id) {
  16. return usersDao.selectByPrimaryKey(id);
  17. }
  18. // 增删改的方法,都需要向消息队列中发送数据
  19. /**
  20. * 删除方法,对外暴露接口
  21. *
  22. * @param id
  23. */
  24. public void deleteByPrimaryKeyPort(Integer id) {
  25. // 将传入的参数放入队列中
  26. StringBuffer buf = new StringBuffer();
  27. buf.append(Constant.DATA_DELETE);
  28. buf.append( ":");
  29. buf.append(String.valueOf(id));
  30. // 调用对应的生产者的sendMessage方法,向队列发送消息
  31. producerServiceUser.sendMessage(destination, buf.toString());
  32. }
  33. public int deleteByPrimaryKey(Integer id) {
  34. return usersDao.deleteByPrimaryKey(id);
  35. }
  36. /**
  37. * 添加方法,对外暴露接口
  38. *
  39. * @param id
  40. */
  41. public void insertPort(Users record) {
  42. // 将传入的参数放入队列中
  43. StringBuffer buf = new StringBuffer();
  44. buf.append(Constant.DATA_INSERT);
  45. buf.append( ":");
  46. buf.append(String.valueOf(record.toString()));
  47. // 调用对应的生产者的sendMessage方法,向队列发送消息
  48. producerServiceUser.sendMessage(destination, buf.toString());
  49. }
  50. public int insert(Users record) {
  51. return usersDao.insert(record);
  52. }
  53. /**
  54. * 添加方法,对外暴露接口
  55. *
  56. * @param id
  57. */
  58. public void insertSelectivePort(Users record) {
  59. // 将传入的参数放入队列中
  60. StringBuffer buf = new StringBuffer();
  61. buf.append(Constant.DATA_INSERT);
  62. buf.append( ":");
  63. buf.append(String.valueOf(record.toString()));
  64. // 调用对应的生产者的sendMessage方法,向队列发送消息
  65. producerServiceUser.sendMessage(destination, buf.toString());
  66. }
  67. public int insertSelective(Users record) {
  68. return usersDao.insertSelective(record);
  69. }
  70. /**
  71. * 更新方法,对外暴露接口
  72. *
  73. * @param id
  74. */
  75. public void updateByPrimaryKeySelectivePort(Users record) {
  76. // 将传入的参数放入队列中
  77. StringBuffer buf = new StringBuffer();
  78. buf.append(Constant.DATA_UPDATE);
  79. buf.append( ":");
  80. buf.append(String.valueOf(record.toString()));
  81. // 调用对应的生产者的sendMessage方法,向队列发送消息
  82. producerServiceUser.sendMessage(destination, buf.toString());
  83. }
  84. public int updateByPrimaryKeySelective(Users record) {
  85. return usersDao.updateByPrimaryKeySelective(record);
  86. }
  87. /**
  88. * 更新方法,对外暴露接口
  89. *
  90. * @param id
  91. */
  92. public void updateByPrimaryKeyPort(Users record) {
  93. // 将传入的参数放入队列中
  94. StringBuffer buf = new StringBuffer();
  95. buf.append(Constant.DATA_UPDATE);
  96. buf.append( ":");
  97. buf.append(String.valueOf(record.toString()));
  98. // 调用对应的生产者的sendMessage方法,向队列发送消息
  99. producerServiceUser.sendMessage(destination, buf.toString());
  100. }
  101. public int updateByPrimaryKey(Users record) {
  102. return usersDao.updateByPrimaryKey(record);
  103. }
  104. }

3.UserServiceDelegate类代码


     
     
     
     
  1. @javax.jws.WebService(targetNamespace = "http://impl.service.store.yundao.com/", serviceName = "UserServiceService", portName = "UserServicePort")
  2. public class UserServiceDelegate {
  3. com.yundao.store.service.impl.UserService userService = new com.yundao.store.service.impl.UserService();
  4. /**
  5. * 对外暴露的接口
  6. */
  7. public Users getUsersByUserCode(String userCode) {
  8. return userService.getUsersByUserCode(userCode);
  9. }
  10. public Users selectByPrimaryKey(Integer id) {
  11. return userService.selectByPrimaryKey(id);
  12. }
  13. /**
  14. * 对外暴露的接口
  15. */
  16. public void deleteByPrimaryKeyPort(Integer id) {
  17. userService.deleteByPrimaryKeyPort(id);
  18. }
  19. @WebMethod(exclude = true)
  20. public int deleteByPrimaryKey(Integer id) {
  21. return userService.deleteByPrimaryKey(id);
  22. }
  23. /**
  24. * 对外暴露的接口
  25. */
  26. public void insertPort(Users record) {
  27. userService.insertPort(record);
  28. }
  29. @WebMethod(exclude = true)
  30. public int insert(Users record) {
  31. return userService.insert(record);
  32. }
  33. /**
  34. * 对外暴露的接口
  35. */
  36. public void insertSelectivePort(Users record) {
  37. userService.insertSelectivePort(record);
  38. }
  39. @WebMethod(exclude = true)
  40. public int insertSelective(Users record) {
  41. return userService.insertSelective(record);
  42. }
  43. /**
  44. * 对外暴露的接口
  45. */
  46. public void updateByPrimaryKeySelectivePort(Users record) {
  47. userService.updateByPrimaryKeySelectivePort(record);
  48. }
  49. @WebMethod(exclude = true)
  50. public int updateByPrimaryKeySelective(Users record) {
  51. return userService.updateByPrimaryKeySelective(record);
  52. }
  53. /**
  54. * 对外暴露的接口
  55. */
  56. public void updateByPrimaryKeyPort(Users record) {
  57. userService.updateByPrimaryKeyPort(record);
  58. }
  59. @WebMethod(exclude = true)
  60. public int updateByPrimaryKey(Users record) {
  61. return userService.updateByPrimaryKey(record);
  62. }
  63. }

4.ProducerServiceUser类


     
     
     
     
  1. **
  2. * 生产者服务类
  3. *
  4. * @author 2018- 3- 26下午 4: 38: 22
  5. * 将消息生产者做成一个服务,当我们需要发送消息的时候,只需要调用ProducerService实例中的sendMessage
  6. * 方法就可以向默认目的发送一个消息。
  7. */
  8. @Service
  9. public class ProducerServiceUser {
  10. @Autowired
  11. private JmsTemplate jmsTemplate;
  12. /**
  13. * 向指定队列发送消息
  14. */
  15. public void sendMessage(Destination destination, final String msg) {
  16. System.out.println(Thread.currentThread().getName() + " 向队列"
  17. + destination.toString() + "发送消息---------------------->" + msg);
  18. jmsTemplate.send(destination, new MessageCreator() {
  19. public Message createMessage(Session session) throws JMSException {
  20. return session.createTextMessage(msg);
  21. }
  22. });
  23. }
  24. /**
  25. * 向默认队列发送消息
  26. */
  27. public void sendMessage(final String msg) {
  28. String destination = jmsTemplate.getDefaultDestinationName();
  29. System.out.println(Thread.currentThread().getName() + " 向队列"
  30. + destination + "发送消息---------------------->" + msg);
  31. jmsTemplate.send( new MessageCreator() {
  32. public Message createMessage(Session session) throws JMSException {
  33. return session.createTextMessage(msg);
  34. }
  35. });
  36. }
  37. }

5.对应的生产者配置文件spring-activemq-producer.xml


     
     
     
     
  1. "1.0" encoding= "UTF-8"?>
  2. "http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/aop"
  4. xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context"
  5. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  9. package= "com.yundao.store.activemq.*" />
  10. "targetConnectionFactory" class= "org.apache.activemq.ActiveMQConnectionFactory">
  11. "brokerURL" value= "tcp://localhost:61616" />
  12. "connectionFactory"
  13. class= "org.springframework.jms.connection.CachingConnectionFactory">
  14. "targetConnectionFactory" ref= "targetConnectionFactory">
  15. "sessionCacheSize" value= "100" />
  16. "userQueueDestination" class= "org.apache.activemq.command.ActiveMQQueue">
  17. "0" value= "queue-user" />
  18. "logQueueDestination" class= "org.apache.activemq.command.ActiveMQQueue">
  19. "0" value= "queue-log" />
  20. "jmsQueueTemplate" class= "org.springframework.jms.core.JmsTemplate">
  21. "connectionFactory" ref= "connectionFactory" />
  22. "receiveTimeout" value= "10000" />

6.对应的消费者配置文件spring-activemq-consumer.xml


     
     
     
     
  1. "1.0" encoding= "UTF-8"?>
  2. "http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/aop"
  4. xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context"
  5. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  9. package= "com.yundao.store.activemq.*" />
  10. "targetConnectionFactory" class= "org.apache.activemq.ActiveMQConnectionFactory">
  11. "brokerURL" value= "tcp://localhost:61616" />
  12. "connectionFactory"
  13. class= "org.springframework.jms.connection.CachingConnectionFactory">
  14. "targetConnectionFactory" ref= "targetConnectionFactory">
  15. "sessionCacheSize" value= "100" />
  16. "userQueueMessageListener"
  17. class= "com.yundao.store.activemq.listener.userQueueMessageListener">
  18. "logQueueMessageListener"
  19. class= "com.yundao.store.activemq.listener.logQueueMessageListener">
  20. "sessionAwareListenerContainer01"
  21. class= "org.springframework.jms.listener.DefaultMessageListenerContainer">
  22. "connectionFactory" ref= "connectionFactory" />
  23. "destination" ref= "userQueueDestination" />
  24. "messageListener" ref= "userQueueMessageListener" />
  25. "sessionAwareListenerContainer02"
  26. class= "org.springframework.jms.listener.DefaultMessageListenerContainer">
  27. "connectionFactory" ref= "connectionFactory" />
  28. "destination" ref= "logQueueDestination" />
  29. "messageListener" ref= "logQueueMessageListener" />

7.监听器的类


     
     
     
     
  1. **
  2. * 配置消息队列监听者(实际项目中采用的是这种方式)
  3. *
  4. * @author 2018- 3- 26下午 4: 46: 44
  5. */
  6. public class userQueueMessageListener implements MessageListener {
  7. private UserService userService;
  8. // 当收到消息后,自动调用该方法
  9. @Override
  10. public void onMessage(Message message) {
  11. TextMessage tm = (TextMessage) message;
  12. try {
  13. System.out.println( "userQueueMessageListener监听到了文本消息:\t" + tm.getText());
  14. //处理业务逻辑
  15. } catch (JMSException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }

8.测试方法


     
     
     
     
  1. /**
  2. * 用户登录
  3. */
  4. public String login() {
  5. setUtfEncoding();
  6. String userCode = getRequest().getParameter( "userCode");
  7. String password = getRequest().getParameter( "password");
  8. // String param = "登录成功";
  9. // producerServiceUser.sendMessage(destination, param);
  10. //
  11. // String param1 = "日志成功";
  12. // producerServiceLog.sendMessage(destination1, param1);
  13. int id= 1;
  14. userService.deleteByPrimaryKeyPort(id);
  15. return SUCCESS;
  16. }

最后能成功监听到调用接口封装的方法传输过来的数据,真实的的接口调用,可以参考我soap调接口那篇

简单的记录下来,毕竟是自己做项目遇到的难点......

你可能感兴趣的:(WebService)