jboss-remoting服务

JBoss Remoting支持多种协议方式调用远程对象,例如socket的方式、RMI的方式、Http的方式等等

分布式框架JBoss Remoting支持多种协议方式调用远程对象,例如socket的方式、RMI的方式、Http的方式等等

目前传输协议有tcp和udp两种,其他http、ftp、telnet等都是基于tcp扩展出来的应用协议

JBoss Remoting 采用了NIO(非阻塞io流,Java NIO可以让你异步的使用IO,例如:当线程从通道读取数据到缓冲区时,线程还是可以进行其他事情。当数据被写入到缓冲区时,线程可以继续处理它。从缓冲区写入通道也类似。)长链接,支持异步调用,采用tcp协议


依赖的jar:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.     <groupId>org.jboss.remoting</groupId>  
  3.     <artifactId>jboss-remoting</artifactId>  
  4.     <version>2.5.3</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>concurrent</groupId>  
  8.     <artifactId>concurrent</artifactId>  
  9.     <version>1.3.4</version>  
  10. </dependency>  
  11. <dependency>  
  12.     <groupId>jboss</groupId>  
  13.     <artifactId>jboss-serialization</artifactId>  
  14.     <version>4.2.2.GA</version>  
  15. </dependency>  

服务端代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.net.MalformedURLException;  
  2.   
  3. import javax.management.MBeanServer;  
  4.   
  5. import org.jboss.remoting.InvocationRequest;  
  6. import org.jboss.remoting.InvokerLocator;  
  7. import org.jboss.remoting.ServerInvocationHandler;  
  8. import org.jboss.remoting.ServerInvoker;  
  9. import org.jboss.remoting.callback.InvokerCallbackHandler;  
  10. import org.jboss.remoting.invocation.NameBasedInvocation;  
  11. import org.jboss.remoting.transport.Connector;  
  12.   
  13. public class ServerTest {  
  14.   
  15.     public static void main(String[] args) {  
  16.         String locatorURI = "socket://127.0.0.1:8084";  
  17.         String params = "/?backlog=50&numAcceptThreads=30&maxPoolSize=30&clientMaxPoolSize=30&clientLeasePeriod=10000&timeout=8000";  
  18.         locatorURI += params;  
  19.         try {  
  20.             InvokerLocator locator = new InvokerLocator(locatorURI);  
  21.             Connector connector = new Connector(locator);  
  22.             connector.create();  
  23.             connector.addInvocationHandler("com.**.jhb.ServerHandle"new ServerInvocationHandler() {  
  24.   
  25.                 @Override  
  26.                 public void addListener(InvokerCallbackHandler arg0) {  
  27.                 }  
  28.   
  29.                 @Override  
  30.                 public Object invoke(InvocationRequest arg0) throws Throwable {  
  31.                     System.out.println("getRequestPayload " + arg0.getRequestPayload());  
  32.                     // 方式一  
  33.                     System.out.println("getParameter " + arg0.getParameter());  
  34.   
  35.                     // 方式二  
  36.                     NameBasedInvocation obj = (NameBasedInvocation) arg0.getParameter();  
  37.                     System.out.println("getParameter " + obj.getParameters()[0]);  
  38.                     return "hello";  
  39.                 }  
  40.   
  41.                 @Override  
  42.                 public void removeListener(InvokerCallbackHandler arg0) {  
  43.                 }  
  44.                 @Override  
  45.                 public void setInvoker(ServerInvoker arg0) {  
  46.                 }  
  47.                 @Override  
  48.                 public void setMBeanServer(MBeanServer arg0) {  
  49.                 }  
  50.             });  
  51.             connector.start();  
  52.         } catch (MalformedURLException e) {  
  53.             e.printStackTrace();  
  54.         } catch (Exception e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.   
  58.     }  
  59.   
  60. }  


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public interface ServerHandle {  
  2.     String say(String hi);  
  3. }  



客户端代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.net.MalformedURLException;  
  2. import java.util.HashMap;  
  3.   
  4. import org.jboss.remoting.Client;  
  5. import org.jboss.remoting.InvokerLocator;  
  6.   
  7. public class ClientTest {  
  8.   
  9.     public static void main(String[] args){  
  10.         String locatorURI = "socket://127.0.0.1:8084";  
  11.         String params = "/?backlog=50&numAcceptThreads=30&maxPoolSize=30&clientMaxPoolSize=30&clientLeasePeriod=10000&timeout=8000";  
  12.         locatorURI += params;  
  13.         try {  
  14.             InvokerLocator locator = new InvokerLocator(locatorURI);  
  15.             //方式1  
  16.             Client client = new Client(locator,ServerHandle.class.getName());  
  17.             client.connect();  
  18.             client.invoke("jhb ",new HashMap<String,String>());  
  19.               
  20.             //方式二  
  21.             /*ServerHandle serverHandle=(ServerHandle) TransporterClient.createTransporterClient(locator, ServerHandle.class); 
  22.             serverHandle.say("jhb");*/  
  23.         } catch (MalformedURLException e) {  
  24.             e.printStackTrace();  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         } catch (Throwable e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.   
  32. }  

参考链接:

http://docs.jboss.org/jbossremoting/2.5.3.SP1/html/chapter-configuration.html#d0e1564

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值