今天碰到了一个很郁闷的问题,运行一个action,在里面需要使用一个java2net对象,完成webservice的调用,action文件如下
package myZone.mvc;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class SendMesAction extends ActionSupport {
private String id;
private String date;
private String type;
private String phoneNum;
private NotifyType nt;
PrintWriter out = null;
public String execute() throws Exception {
File file=new File("/opt/1.txt");
file.createNewFile();
JavaNet jn = new JavaNet();
File file2=new File("/opt/2.txt");
file2.createNewFile();
@SuppressWarnings("unused")
String flghtid = id.substring(2);
if (type.endsWith("jj")) {
nt = NotifyType.JJNotify;
} else {
nt = NotifyType.CJNotify;
}
jn.sendMes(phoneNum, id, date, nt);
setOut();
if (phoneNum.length() != 11) {
out.print("false");
} else {
out.print("success");
}
out.flush();
out.close();
return null;
}
public void writeFile(String filePath,String fileName,String args) throws IOException
{
FileWriter fw = new FileWriter(filePath+fileName);
PrintWriter out=new PrintWriter(fw);
out.write(args);
out.println();
//out.flush();
fw.close();
out.close();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public PrintWriter getOut() {
return out;
}
public void setOut() throws IOException {
this.out = ServletActionContext.getResponse().getWriter();
}
public NotifyType getNt() {
return nt;
}
public void setNt(NotifyType nt) {
this.nt = nt;
}
}
java2net文件如下
package com.SMSService;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.holders.BooleanHolder;
import javax.xml.rpc.holders.StringHolder;
public class Java2Net {
public Java2Net() throws IOException{
File file=new File("/opt/3.txt");
file.createNewFile();
}
public void sendMes(String phoneNum, String id, String time, NotifyType type) throws ServiceException, RemoteException{
CSMSService service = new CSMSServiceLocator();
CSMSServiceSoap cs = service.getCSMSServiceSoap();
BooleanHolder result = new BooleanHolder();
StringHolder rpt = new StringHolder();
cs.preCustomNotify(phoneNum, id, time,
type, result, rpt);
}
}
在我本地调试的时候一切正常,然而部署到linux服务器上之后,只能生成1.txt,也就是说明new java2net对象时,连他的构造方法都没有走,试过各种方法,更改类名、注释内部声明方法等,均无效果,后来突然想到会不会是j2ee的jar有问题,经过覆盖,解决了问题。
这个问题告诉我们,java在生成一个对象的时候是先编译所import的java类,然后才执行构造函数的。如果一个类本身没有什么问题,就是不能实例化,应该先考虑所import的java对象是否有问题。
package myZone.mvc;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class SendMesAction extends ActionSupport {
private String id;
private String date;
private String type;
private String phoneNum;
private NotifyType nt;
PrintWriter out = null;
public String execute() throws Exception {
File file=new File("/opt/1.txt");
file.createNewFile();
JavaNet jn = new JavaNet();
File file2=new File("/opt/2.txt");
file2.createNewFile();
@SuppressWarnings("unused")
String flghtid = id.substring(2);
if (type.endsWith("jj")) {
nt = NotifyType.JJNotify;
} else {
nt = NotifyType.CJNotify;
}
jn.sendMes(phoneNum, id, date, nt);
setOut();
if (phoneNum.length() != 11) {
out.print("false");
} else {
out.print("success");
}
out.flush();
out.close();
return null;
}
public void writeFile(String filePath,String fileName,String args) throws IOException
{
FileWriter fw = new FileWriter(filePath+fileName);
PrintWriter out=new PrintWriter(fw);
out.write(args);
out.println();
//out.flush();
fw.close();
out.close();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public PrintWriter getOut() {
return out;
}
public void setOut() throws IOException {
this.out = ServletActionContext.getResponse().getWriter();
}
public NotifyType getNt() {
return nt;
}
public void setNt(NotifyType nt) {
this.nt = nt;
}
}
java2net文件如下
package com.SMSService;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.holders.BooleanHolder;
import javax.xml.rpc.holders.StringHolder;
public class Java2Net {
public Java2Net() throws IOException{
File file=new File("/opt/3.txt");
file.createNewFile();
}
public void sendMes(String phoneNum, String id, String time, NotifyType type) throws ServiceException, RemoteException{
CSMSService service = new CSMSServiceLocator();
CSMSServiceSoap cs = service.getCSMSServiceSoap();
BooleanHolder result = new BooleanHolder();
StringHolder rpt = new StringHolder();
cs.preCustomNotify(phoneNum, id, time,
type, result, rpt);
}
}
在我本地调试的时候一切正常,然而部署到linux服务器上之后,只能生成1.txt,也就是说明new java2net对象时,连他的构造方法都没有走,试过各种方法,更改类名、注释内部声明方法等,均无效果,后来突然想到会不会是j2ee的jar有问题,经过覆盖,解决了问题。
这个问题告诉我们,java在生成一个对象的时候是先编译所import的java类,然后才执行构造函数的。如果一个类本身没有什么问题,就是不能实例化,应该先考虑所import的java对象是否有问题。