WebService学習(八)--webseviceインタフェースを利用してactivemqの生産者をカプセル化し、ニュースを監視する

215734 ワード

このシリーズの第5編では、myeclipseを利用してjax-wsベースのインタフェースを生成し、wsdlに正常にアクセスした後、他の操作を続行します.
1.サービスインタフェース:

     
     
     
     
  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

, ......