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>