本系列第五篇,利用myeclipse生成基于jax-ws的接口,并能成功访问wsdl后,接下其他操作
1.service接口:
-
/**
-
* 用于外部系统调用接口
-
*
-
* @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调接口那篇
简单的记录下来,毕竟是自己做项目遇到的难点......