package com.wxgk.xfirespring;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class TestClient {
public static void main(String[] args) {
// demo1();
// try {
// demo2();
// } catch (MalformedURLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// demo3();
// try {
// demo4();
// } catch (MalformedURLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// demo5();
// try {
// demo6();
// } catch (MalformedURLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
demo7();
// demo8();
}
public static void demo1() {
Service serviceModel = new ObjectServiceFactory()
.create(IBookService.class);
String url = "http://localhost:8888/xfilespring/service/BookService";
IBookService service = null;
try {
service = (IBookService) new XFireProxyFactory().create(
serviceModel, url);
Book b = service.getBook();
System.out.println(b.getName() + ">>>>>>>>Client: " + b.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void demo2() throws MalformedURLException, Exception {
Client client = new Client(new URL(
"http://localhost:8888/xfilespring/service/BookService?wsdl"));
Object[] results = client.invoke("getBook", new Object[] {});
System.out.println(results[0]);
}
// 查询
public static void demo3() {
Service serviceModel = new ObjectServiceFactory()
.create(IBookService.class);
String url = "http://localhost:8888/xfilespring/service/BookService";
IBookService service = null;
try {
service = (IBookService) new XFireProxyFactory().create(
serviceModel, url);
List<Book> blists = service.findAllBooks();
for (Book b : blists) {
System.out
.println(b.getName() + ">>>>>>>>Client: " + b.getId());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void demo4() throws MalformedURLException, Exception {
Client client = new Client(new URL(
"http://localhost:8888/xfilespring/service/BookService?wsdl"));
Object[] results = client.invoke("findAllBooks", new Object[] {});
System.out.println("实现类:" + results[0].getClass().getName()
+ " ********** ");
System.out.println(" :" + results[0].getClass());
// System.out.print("\n"+"subject实现的接口是:");
// Class<?>[] interfaces=results[0].getClass().getInterfaces();
// System.out.println(interfaces.getClass().getName());
}
// employee
public static void demo5() {
Service serviceModel = new ObjectServiceFactory()
.create(IEmployeeService.class);
String url = "http://localhost:8888/xfilespring/service/EmployeeService";
IEmployeeService service = null;
try {
service = (IEmployeeService) new XFireProxyFactory().create(
serviceModel, url);
List<Employee> elists = service.findById(2);
for (Employee e : elists) {
System.out.println(e.getEmpAddress() + ">>>>>>>>Client: "
+ e.getId() + " ********** " + e.getEmpName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void demo6() throws MalformedURLException, Exception {
Client client = new Client(
new URL(
"http://localhost:8888/xfilespring/service/EmployeeService?wsdl"));
Object[] results = client.invoke("findById", new Object[] { 1 });
System.out.println("实现类:" + results[0].getClass().getName()
+ " ********** ");
System.out.println(" :" + results[0].getClass());
}
// myservice
public static void demo7() {
Service serviceModel = new ObjectServiceFactory()
.create(IMyService.class);
String url = "http://localhost:8888/xfilespring/service/MyService";
IMyService service = null;
try {
service = (IMyService) new XFireProxyFactory().create(serviceModel,
url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int result = service.add(1, 4);
System.out.println("result: " + result);
}
public static void demo8() {
Client client=null;
try {
client = new Client(new URL(
"http://localhost:8888/xfilespring/service/MyService?wsdl"));
Object[] results = client.invoke("add", new Object[] { 1, 3 });
System.out.println("结果:" + results[0] + " ********** ");
System.out.println(" :" + results[0].getClass());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}