xfire实现CS架构的服务发布

本文通过实例详细讲解了使用XFIRE旧版和CXF新版进行服务发布的步骤,包括服务端和客户端的创建。在新版CXF示例中,介绍了如何利用注解简化服务发布,并给出了报错处理的提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、旧版CXF示例

1.新建普通JAVA工程,导入XFIRE用户库和xfire-all-1.2.6.jar.新建服务类接口IHelloService,接口实现类HelloService(实现接口方法public String hello(String name))

2.模拟服务发布类:EmbeddedService,其内容如下:
 public boolean start(){
  ObjectServiceFactory sf=new ObjectServiceFactory();
  Service service=sf.create(IHelloService.class);
  service.setInvoker(new BeanInvoker(new HelloService())); 
  XFire xfire=XFireFactory.newInstance().getXFire();
  xfire.getServiceRegistry().register(service); 
  XFireHttpServer server=new XFireHttpServer();
  server.setPort(8191);
  server.start();//这里要加异常try- catch就返回false 
  return true; }

3.启动服务端, 在浏览器中运行:http://localhost:8191---就可以看到服务是否发布成功.
public static void main(String[] args) {
  EmbeddedService es=new EmbeddedService();
  es.start();

4.新建客户端普通JAVA工程,也要导入XFIRE.客户端类XFireClient,其内容如下:
public static void main(String[] args) throws MalformedURLException, Exception {
  Client client=new Client(new URL("http://localhost:8191/IHelloService?wsdl"));
  Object[] results=client.invoke("hello", new Object[]{"admin"});
  System.out.println(results[0].toString());  }


二、新版CXF示例:


apache cxf官网(http://cxf.apache.org/)下载:
http://cxf.apache.org/download.html
http://www.apache.org/dyn/closer.lua/cxf/3.0.8/apache-cxf-3.0.8.zip

1. 新建接口类:

package com.caoyong.cxf.demo.inf;


import javax.jws.WebService;


import com.caoyong.cxf.demo.server.HelloWordImpl;
import com.caoyong.cxf.demo.util.ServerClassImpl;


@WebService
@ServerClassImpl(serverImplClass = HelloWordImpl.class) //这个后面封闭WEB发布方式才需要.
public interface IHelloWord
{
String sayHello(String message);
}


2. 新建实现类

package com.caoyong.cxf.demo.server;


import com.caoyong.cxf.demo.inf.IHelloWord;

@WebService
public class HelloWordImpl implements IHelloWord
{


@Override
public String sayHello(String message)
{
String s = "caoyong:" + message;
System.out.println("HelloWordImpl sayHello :" + s);
return s;
}


}


3. 新建常量类:

package com.caoyong.cxf.demo.constant;


public class ConfigConstant
{
public final static String IP = "127.0.0.1";

public final static String PORT = "9998";

public final static String SERVER_NAME = "DEMO";

public static String getBaseUrl()
{
return "http://" + IP + ":" + PORT + "/" + SERVER_NAME;
}
}


4. 新建annotation工具类:

package com.caoyong.cxf.demo.util;


import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ServerClassImpl
{
Class<?> serverImplClass();
}


5. 新建工具服务类:

package com.caoyong.cxf.demo.util;


import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;


import com.caoyong.cxf.demo.constant.ConfigConstant;
import com.caoyong.cxf.demo.inf.IHelloWord;


public class WebServerUtil
{
public static void regsister(Class<?> c)
{
ServerClassImpl implAnno = c.getAnnotation(ServerClassImpl.class);
if (null != implAnno)
{
Class<?> impl = implAnno.serverImplClass();
if (null != impl)
{
JaxWsServerFactoryBean serverFactory = new JaxWsServerFactoryBean();
serverFactory.setServiceClass(c);
String address = ConfigConstant.getBaseUrl() + "/" + c.getSimpleName();
serverFactory.setAddress(address);
try
{
serverFactory.setServiceBean(impl.newInstance());
serverFactory.create();
System.out.println(c.getSimpleName() + " cxf server start!!!");
} catch (InstantiationException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
}


}
} else
{
System.out.println("service not exists");
}


}

public static <T> T getServer(Class<T> c)
{
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
String address = ConfigConstant.getBaseUrl() + "/" + IHelloWord.class.getSimpleName();
proxyFactory.setAddress(address);
proxyFactory.setServiceClass(c);

return proxyFactory.create(c);
}
}


6. 服务发布类:

package com.caoyong.cxf.demo.test;


import org.apache.cxf.jaxws.JaxWsServerFactoryBean;


import com.caoyong.cxf.demo.constant.ConfigConstant;
import com.caoyong.cxf.demo.inf.IHelloWord;
import com.caoyong.cxf.demo.server.HelloWordImpl;
import com.caoyong.cxf.demo.util.WebServerUtil;


public class CxfServerDemo
{


public static void main(String[] args)
{
test1();
}

// 原始发布方式
private static void test1()
{
JaxWsServerFactoryBean serverFactory = new JaxWsServerFactoryBean();
serverFactory.setServiceClass(IHelloWord.class);
String address = ConfigConstant.getBaseUrl() + "/" + IHelloWord.class.getSimpleName();
serverFactory.setAddress(address);
serverFactory.setServiceBean(new HelloWordImpl());
serverFactory.create();
System.out.println("cxf server start!!!");
}

// 调用封装方式发布
private static void test2()
{
WebServerUtil.regsister(IHelloWord.class);
}


}


7. 客户端方式调用:

package com.caoyong.cxf.demo.test;


import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


import com.caoyong.cxf.demo.constant.ConfigConstant;
import com.caoyong.cxf.demo.inf.IHelloWord;
import com.caoyong.cxf.demo.util.WebServerUtil;


public class CxfClientDemo
{


public static void main(String[] args)
{
test2();
}

// 普通方式 调用
private static void test1()
{
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
String address = ConfigConstant.getBaseUrl() + "/" + IHelloWord.class.getSimpleName();
proxyFactory.setAddress(address);
proxyFactory.setServiceClass(IHelloWord.class);
IHelloWord helloWord = proxyFactory.create(IHelloWord.class);
String s = helloWord.sayHello("how are you");
System.out.println(s);
}

// 封装方式调用服务.
private static void test2()
{
IHelloWord helloWord = WebServerUtil.getServer(IHelloWord.class);
System.out.println("test2:" + helloWord.sayHello("abc"));
}


}


CXF - prefix wsdp is not bound to a namespace - with linked exception - 报错处理


应该是接口服务实现类上没有加标注@WebService。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值