最近写一个手机短信功能。就是把GSM接入到自己的系统中。在开发的过程中用到了,webservice和多线程知识。
因为之前没有接触过webservice,所以花了两天时间搞这个。在研究的过程中也是遇到了不少的问题。最终经过查资料,请教高人搞了了
来。今天把我的心得记下来,防止时间长了忘记,同时也能给想要学习webservice的同仁,提供一点参考。
费话少说,下面进入正题:
1.资源准备: axis2-1.5-bin.zip , axis2-1.5-war.zip 两个包。可以在apahce网站上下到最新版本。目前1.5是最新版本。
axis2-1.5-bin.zip解压后 如下图所示:lib包下包含axis2用到的所有jar包。samples是一些示例,学习的时候可以参考。
将axis2-1.5-war.zip 文件解压到相应的目录,将目录中的axis2.war文件放到<Tomcat安装目录>/webapps目录中(本文使用的Tomcat的版本是6.x),并启动Tomcat,可以直接运行。
启动toamct后,在IE中输入http://localhost:8080/axis2/ 看到如下界面,表示安装成功。
2. axis2安装成功后,就可以编写Webservice工程了。服务器端工程和正常的java工程一样编写,不要有任何顾及。java工程可以很轻松的发布成一个Webservice工程。
3. Webservice工程发布。正如我在2种所说,开发的时候,按正常的java工程去开发就可以。主要是在于发布。
我在学习的时候,上网查到的资料大部分是对一个java类的发布与测试,基本上没看见如何把一个Java工程发布置成Webservice工程。
后来发现其实很简单。
1). 把所有的java类文件,包括一些配置文件放置到 axis2/WEB-INF/classes 下。如下图:
我的类文件全在com文件包下,org文件是hibernate用到的类文件,如HibernateUtil类。
2). 把你工程中用到的jar包拷到 axis2/WEB-INF/lib 包下。该包下已经有axis2用到的所有jar包,只需把你工程中用到的其它包拷进去。
3). 把 meta-inf/services.xml 打成arr包。具体做法如下: 到运行下cd →粘贴classes路径回车→输入 jar cvf mobile.jar ./* →回车。把生成的mobile.jar 改名成为 mobile.aar(当然不改也行,改了较正规) ,然后拷入到 axis2/WEB-INF/services下。如下:
4).启动tomact服务,在IE中输入 http://localhost:8080/axis2/services/listServices ,便可看到我刚发布我的服务。如下图所示:
点服务名“Mobile”,可以查看wsdl文件。如下图所示。客户端调用的时候,就是根据wsdl文件调用。
上面讲述了如何发布Webservice工程。下面我来说一下,编写Webservice工程比较关键的几个点
1. 数据传递。 webservice 支持很多类型数据的传递,但对Date类型数据传递支持不好。我刚开始在传递对象时,对象中有一个Date类型的数据,怎么也传不过去,并且报错。后来才发现这个问题,改成String就好了。
axis2 可以传对象,对象数组。集合类型的数据必须放在对象中,作为一个属性传递,直接传是传不过去的。
2. 我的服务端接收类代码如下;
package com.css.mobile.mobilemsg;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.css.mobile.config.CommonConstaints;
import com.css.mobile.exception.ExceptionConfig;
import com.css.mobile.hibernate.dao.CommonDAO;
import com.css.mobile.hibernate.model.Phone;
import com.css.mobile.pojo.data.PhoneVO;
import com.css.mobile.resource.MobileMsg;
import com.css.mobile.resource.Phones;
import com.css.mobile.resource.Resource;
import com.css.mobile.thread.StartThread;
/**
* ReceiveMsg WebService对外接口类,用于接收客户端传过来的短信
* @author lizhh
*/
public class ReceiveMsg {
private static Log log = LogFactory.getLog(ReceiveMsg.class);
static{
StartThread.getInstance().startThread();
}
// 对外接口,用于接收客户端发过来的短信
public String receiveMsg(Phones phones){
String result = ExceptionConfig.getString("systemMsg.sendMsgError");
if(phones == null){
result = ExceptionConfig.getString("systemMsg.paramError");
}else{
PhoneVO[] list = phones.getList();
saveResource(Arrays.asList(list));
result = ExceptionConfig.getString("systemMsg.sendMsgSuccess");
}
return result;
}
/**
* 把短信放入数据库中
* @param phone 短信VO
*/
private void setMsgToDataBase(Phone phone){
phone.setPhoneDate(Calendar.getInstance().getTime());
CommonDAO.getInstance().add(phone);
}
/**
* 如果队列被加锁则不放入队列,如果没被加锁则放入队列
* @param phone 短信VO
*/
private void saveResource(List list){
Resource resource = MobileMsg.resourceMap.get(CommonConstaints.MAINTHREAD);
if(list !=null && !list.isEmpty()){
for(int i=0;i<list.size();i++){
PhoneVO phonevo = (PhoneVO)list.get(i);
Phone phone = setPhoneData(phonevo);
if(resource.isAlive()){
if(resource.isLock()){
phone.setPhonestate(CommonConstaints.notinQUEUE);
}else{
resource.setLock(CommonConstaints.LOCK);
phone.setPhonestate(CommonConstaints.NOSEND);
// 加入队列
resource.getPhoneList().add(phone);
resource.setLock(CommonConstaints.NOTLOCK);
}
}
// 存入数据库
setMsgToDataBase(phone);
}
}
}
public Phone setPhoneData(PhoneVO phonevo){
Phone phone = new Phone();
phone.setPhoneContent(phonevo.getPhoneContent());
phone.setPnoneNumber(phonevo.getPnoneNumber());
phone.setPhonestate(phonevo.getPhonestate());
return phone;
}
}
3. 客户端调用类如下:
package com.css.mobile.client;
import java.util.Calendar;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.css.mobile.hibernate.model.Phone;
import com.css.mobile.pojo.data.PhoneVO;
import com.css.mobile.resource.Phones;
public class ExampleClient {
@SuppressWarnings("deprecation")
public static void main(String[] args)throws Exception{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getoptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/Mobile?wsdl");
options.setTo(targetEPR);
options.setTimeOutInMilliSeconds(60000);
/**
* 客户端invoke方法的参数一:QName(发布方法的名称)
*/
QName opQueryResult = new QName("http://mobilemsg.mobile.css.com","receiveMsg");
/**
* 客户端invoke方法的参数二:Object[](发布方法的参数数组)
*/
Phones phones = new Phones();
java.util.List<PhoneVO> phoneList = new java.util.ArrayList<PhoneVO>();
PhoneVO phone1 = new PhoneVO();
phone1.setPnoneNumber("15110092285");
phone1.setPhoneContent("第一999aaa个短信测试");
phone1.setPhonestate("0");
phoneList.add(phone1);
phones.setList(new PhoneVO[]{phone1});
Object[] opQueryResultArgs = new Object[] {phones};
/**
* 客户端invoke方法的参数三:Class[](发布方法的返回类型)
*/
Class[] returnTypes = new Class[] {String.class };
/**
* 调用客户端的invoke方法
*/
Object[] response=serviceClient.invokeBlocking(opQueryResult,opQueryResultArgs,returnTypes);
String result=(String)response[0];
System.out.println(result);
}
}
上面代三中,Phones是一个对象,Phones中包括一个数组属性。
4. services.xml 代码如下: 该文件一定要放在 meta-inf 文件夹下。发布时一起打成aar包。
<?xml version="1.0" encoding="UTF-8"?>
<service name="Mobile">
<parameter name="ServiceClass">com.css.mobile.mobilemsg.ReceiveMsg
</parameter>
<operation name="receiveMsg">
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPcmessageReceiver" />
<actionMapping>urn:receiveMsg</actionMapping>
</operation>
</service>
我在上面侧重于讲解如何把一个java工程,发布成一个webservice工程,以及客户端如何调用服务端。还有就是数据传输问题。
至于axis2 (webservice) 我就不做详细介绍了。我在学习的过程,找到了一本很不错的书,推荐给大家,网址是:http://www.blogjava.net/nokiaguy/archive/2009/04/archive/2009/nokiaguy/archive/2009/nokiaguy/archive/2009/01/02/249556.html
这本书介绍的非常详细。大家一定要多看。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。