RMI远程调用

1。服务端

接口:

public interface CrackClient
{

 // 获得图片
 public byte[] getCrack();
 // 获得包长度
 public int getPackLength(byte[] p);

}

实现类:


public class CrackClientImp implements CrackClient
{
 // 获得破解开
 public byte[] getCrack()
 {
  byte[] defaultvalue = "".getBytes();
  byte[] code = null;
  
  CrackControl c = CrackControl.getInstance();
  CrackBean bean = c.take();
  if (bean != null)
  {
   byte[] name = bean.getTypeName().getBytes();
   byte[] pack = ConvertUtils.intToBytes4(name.length);
   byte[] content = bean.getContent();
   code = new byte[content.length + pack.length + name.length];
   System.arraycopy(pack, 0, code, 0, pack.length);
   System.arraycopy(name, 0, code, pack.length, name.length);
   System.arraycopy(content, 0, code, pack.length + name.length, content.length);
  }
  if(code==null){
   //throw new RuntimeException("Exception in getCrack()");
   code = defaultvalue;   
  }
  return code;
 } 
 public int getPackLength(byte[] p)
 {
  int value = 0;
  if (p.length >= 4)
  {
   byte[] bytes = new byte[4];
   System.arraycopy(p, 0, bytes, 0, 4);
   value = ConvertUtils.byteToInt(bytes);
  }
  return value;
 }

}

配置文件applicationContext:

<bean id="TraceService" class="com.ue.ws.CrackClientImp"></bean>
 <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="serviceName" value="TraceService"></property>
  <property name="service" ref="TraceService"></property>
  <property name="serviceInterface" value="com.ue.ws.CrackClient"></property>
  <property name="registryPort" value="2299"></property>
 </bean> 

 

2.客户端

接口:

public interface CrackClient
{

 // 获得图片
 public byte[] getCrack();
 // 获得包长度
 public int getPackLength(byte[] p);

}
配置文件server.xml:

<bean id="trackclient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  <property name="serviceUrl" value="rmi://服务器IP:2299/TraceService"></property>
  <property name="serviceInterface" value="com.ue.test.CrackClient"></property>
 </bean>

测试类:

public class TestCrack {

 /**
  * @param args
  */
 public static void main(String[] args) {
  ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:server.xml");
  CrackClient obj = (CrackClient)ctx.getBean("trackclient");
  obj.unCrack("");
 }

}

 

这个例子是rmi集成spring的,服务器端是web工程,在需要在tomcat中启动

在web.xml里面还要配置:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class> org.springframework.web.context.ContextLoaderListener
   </listener-class>
 </listener>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值