WebService学習(八)--webseviceインタフェースを利用してactivemqの生産者をカプセル化し、ニュースを監視する
215734 ワード
このシリーズの第5編では、myeclipseを利用してjax-wsベースのインタフェースを生成し、wsdlに正常にアクセスした後、他の操作を続行します.
1.サービスインタフェース:
1.サービスインタフェース:
-
/**
-
*
-
*
-
* @author Administrator
-
*
-
*/
-
public
interface IUserService {
-
-
public Users getUsersByUserCode(String userCode);
-
-
public Users selectByPrimaryKey(Integer id);
-
-
public int deleteByPrimaryKey(Integer id);
-
-
public int insert(Users record);
-
-
public int insertSelective(Users record);
-
-
public int updateByPrimaryKeySelective(Users record);
-
-
public int updateByPrimaryKey(Users record);
-
-
}
2.UserService
-
/**
-
*
-
*/
-
@Service
-
public
class UserService implements IUserService {
-
private UsersDao usersDao;
-
-
@Resource
-
private ProducerServiceUser producerServiceUser;
-
-
@Resource(name =
"userQueueDestination")
-
private Destination destination;
-
-
//
-
public Users getUsersByUserCode(String userCode) {
-
return usersDao.getUsersByUserCode(userCode);
-
}
-
-
public Users selectByPrimaryKey(Integer id) {
-
return usersDao.selectByPrimaryKey(id);
-
}
-
-
// ,
-
/**
-
* ,
-
*
-
* @param id
-
*/
-
public void deleteByPrimaryKeyPort(Integer id) {
-
//
-
StringBuffer buf =
new StringBuffer();
-
buf.append(Constant.DATA_DELETE);
-
buf.append(
":");
-
buf.append(String.valueOf(id));
-
// sendMessage ,
-
producerServiceUser.sendMessage(destination, buf.toString());
-
}
-
-
public int deleteByPrimaryKey(Integer id) {
-
return usersDao.deleteByPrimaryKey(id);
-
}
-
-
/**
-
* ,
-
*
-
* @param id
-
*/
-
-
public void insertPort(Users record) {
-
//
-
StringBuffer buf =
new StringBuffer();
-
buf.append(Constant.DATA_INSERT);
-
buf.append(
":");
-
buf.append(String.valueOf(record.toString()));
-
// sendMessage ,
-
producerServiceUser.sendMessage(destination, buf.toString());
-
}
-
-
public int insert(Users record) {
-
return usersDao.insert(record);
-
}
-
-
/**
-
* ,
-
*
-
* @param id
-
*/
-
public void insertSelectivePort(Users record) {
-
//
-
StringBuffer buf =
new StringBuffer();
-
buf.append(Constant.DATA_INSERT);
-
buf.append(
":");
-
buf.append(String.valueOf(record.toString()));
-
// sendMessage ,
-
producerServiceUser.sendMessage(destination, buf.toString());
-
}
-
-
public int insertSelective(Users record) {
-
return usersDao.insertSelective(record);
-
}
-
-
/**
-
* ,
-
*
-
* @param id
-
*/
-
-
public void updateByPrimaryKeySelectivePort(Users record) {
-
//
-
StringBuffer buf =
new StringBuffer();
-
buf.append(Constant.DATA_UPDATE);
-
buf.append(
":");
-
buf.append(String.valueOf(record.toString()));
-
// sendMessage ,
-
producerServiceUser.sendMessage(destination, buf.toString());
-
}
-
-
public int updateByPrimaryKeySelective(Users record) {
-
return usersDao.updateByPrimaryKeySelective(record);
-
}
-
-
/**
-
* ,
-
*
-
* @param id
-
*/
-
-
public void updateByPrimaryKeyPort(Users record) {
-
//
-
StringBuffer buf =
new StringBuffer();
-
buf.append(Constant.DATA_UPDATE);
-
buf.append(
":");
-
buf.append(String.valueOf(record.toString()));
-
// sendMessage ,
-
producerServiceUser.sendMessage(destination, buf.toString());
-
}
-
-
public int updateByPrimaryKey(Users record) {
-
return usersDao.updateByPrimaryKey(record);
-
}
-
-
}
3.UserServiceDelegate
-
@javax.jws.WebService(targetNamespace =
"http://impl.service.store.yundao.com/", serviceName =
"UserServiceService", portName =
"UserServicePort")
-
public
class UserServiceDelegate {
-
-
com.yundao.store.service.impl.UserService userService =
new com.yundao.store.service.impl.UserService();
-
-
/**
-
*
-
*/
-
public Users getUsersByUserCode(String userCode) {
-
return userService.getUsersByUserCode(userCode);
-
}
-
-
public Users selectByPrimaryKey(Integer id) {
-
return userService.selectByPrimaryKey(id);
-
}
-
-
/**
-
*
-
*/
-
public void deleteByPrimaryKeyPort(Integer id) {
-
userService.deleteByPrimaryKeyPort(id);
-
}
-
-
@WebMethod(exclude =
true)
-
public int deleteByPrimaryKey(Integer id) {
-
return userService.deleteByPrimaryKey(id);
-
}
-
-
/**
-
*
-
*/
-
public void insertPort(Users record) {
-
userService.insertPort(record);
-
}
-
-
@WebMethod(exclude =
true)
-
public int insert(Users record) {
-
return userService.insert(record);
-
}
-
-
/**
-
*
-
*/
-
public void insertSelectivePort(Users record) {
-
userService.insertSelectivePort(record);
-
}
-
-
@WebMethod(exclude =
true)
-
public int insertSelective(Users record) {
-
return userService.insertSelective(record);
-
}
-
-
/**
-
*
-
*/
-
public void updateByPrimaryKeySelectivePort(Users record) {
-
userService.updateByPrimaryKeySelectivePort(record);
-
}
-
-
@WebMethod(exclude =
true)
-
public int updateByPrimaryKeySelective(Users record) {
-
return userService.updateByPrimaryKeySelective(record);
-
}
-
-
/**
-
*
-
*/
-
public void updateByPrimaryKeyPort(Users record) {
-
userService.updateByPrimaryKeyPort(record);
-
}
-
-
@WebMethod(exclude =
true)
-
public int updateByPrimaryKey(Users record) {
-
return userService.updateByPrimaryKey(record);
-
}
-
-
}
4.ProducerServiceUser
-
**
-
*
-
*
-
*
@author
2018-
3-
26
4:
38:
22
-
* , , ProducerService sendMessage
-
* 。
-
*/
-
@Service
-
public
class ProducerServiceUser {
-
@Autowired
-
private JmsTemplate jmsTemplate;
-
-
/**
-
*
-
*/
-
public void sendMessage(Destination destination, final String msg) {
-
System.out.println(Thread.currentThread().getName() +
" "
-
+ destination.toString() +
" ---------------------->" + msg);
-
jmsTemplate.send(destination,
new MessageCreator() {
-
public Message createMessage(Session session) throws JMSException {
-
return session.createTextMessage(msg);
-
}
-
});
-
}
-
-
/**
-
*
-
*/
-
public void sendMessage(final String msg) {
-
String destination = jmsTemplate.getDefaultDestinationName();
-
System.out.println(Thread.currentThread().getName() +
" "
-
+ destination +
" ---------------------->" + msg);
-
jmsTemplate.send(
new MessageCreator() {
-
public Message createMessage(Session session) throws JMSException {
-
return session.createTextMessage(msg);
-
}
-
});
-
}
-
-
}
5. spring-activemq-producer.xml
-
"1.0" encoding=
"UTF-8"?>
-
-
"http://www.springframework.org/schema/beans"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance" xmlns:aop=
"http://www.springframework.org/schema/aop"
-
xmlns:tx=
"http://www.springframework.org/schema/tx" xmlns:context=
"http://www.springframework.org/schema/context"
-
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
-
-
-
-
package=
"com.yundao.store.activemq.*" />
-
-
-
-
"targetConnectionFactory"
class=
"org.apache.activemq.ActiveMQConnectionFactory">
-
"brokerURL" value=
"tcp://localhost:61616" />
-
-
-
-
"connectionFactory"
-
class=
"org.springframework.jms.connection.CachingConnectionFactory">
-
"targetConnectionFactory" ref=
"targetConnectionFactory">
-
-
"sessionCacheSize" value=
"100" />
-
-
-
-
"userQueueDestination"
class=
"org.apache.activemq.command.ActiveMQQueue">
-
-
"0" value=
"queue-user" />
-
-
-
"logQueueDestination"
class=
"org.apache.activemq.command.ActiveMQQueue">
-
-
"0" value=
"queue-log" />
-
-
-
-
-
"jmsQueueTemplate"
class=
"org.springframework.jms.core.JmsTemplate">
-
"connectionFactory" ref=
"connectionFactory" />
-
"receiveTimeout" value=
"10000" />
-
-
-
-
6. spring-activemq-consumer.xml
-
"1.0" encoding=
"UTF-8"?>
-
-
"http://www.springframework.org/schema/beans"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance" xmlns:aop=
"http://www.springframework.org/schema/aop"
-
xmlns:tx=
"http://www.springframework.org/schema/tx" xmlns:context=
"http://www.springframework.org/schema/context"
-
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
-
-
-
-
package=
"com.yundao.store.activemq.*" />
-
-
-
"targetConnectionFactory"
class=
"org.apache.activemq.ActiveMQConnectionFactory">
-
"brokerURL" value=
"tcp://localhost:61616" />
-
-
-
-
"connectionFactory"
-
class=
"org.springframework.jms.connection.CachingConnectionFactory">
-
"targetConnectionFactory" ref=
"targetConnectionFactory">
-
-
"sessionCacheSize" value=
"100" />
-
-
-
-
-
-
"userQueueMessageListener"
-
class=
"com.yundao.store.activemq.listener.userQueueMessageListener">
-
-
"logQueueMessageListener"
-
class=
"com.yundao.store.activemq.listener.logQueueMessageListener">
-
-
-
-
"sessionAwareListenerContainer01"
-
class=
"org.springframework.jms.listener.DefaultMessageListenerContainer">
-
"connectionFactory" ref=
"connectionFactory" />
-
"destination" ref=
"userQueueDestination" />
-
"messageListener" ref=
"userQueueMessageListener" />
-
-
-
-
"sessionAwareListenerContainer02"
-
class=
"org.springframework.jms.listener.DefaultMessageListenerContainer">
-
"connectionFactory" ref=
"connectionFactory" />
-
"destination" ref=
"logQueueDestination" />
-
"messageListener" ref=
"logQueueMessageListener" />
-
-
-
-
7.
-
**
-
* ( )
-
*
-
*
@author
2018-
3-
26
4:
46:
44
-
*/
-
public
class userQueueMessageListener implements MessageListener {
-
private UserService userService;
-
// ,
-
@Override
-
public void onMessage(Message message) {
-
TextMessage tm = (TextMessage) message;
-
try {
-
System.out.println(
"userQueueMessageListener :\t" + tm.getText());
-
//
-
}
catch (JMSException e) {
-
e.printStackTrace();
-
}
-
-
}
-
-
}
8.
-
/**
-
*
-
*/
-
public String login() {
-
setUtfEncoding();
-
String userCode = getRequest().getParameter(
"userCode");
-
String password = getRequest().getParameter(
"password");
-
-
// String param = " ";
-
// producerServiceUser.sendMessage(destination, param);
-
//
-
// String param1 = " ";
-
// producerServiceLog.sendMessage(destination1, param1);
-
int id=
1;
-
userService.deleteByPrimaryKeyPort(id);
-
return SUCCESS;
-
}
, , soap
, ......