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:
- <dependency>
- <groupId>org.jboss.remoting</groupId>
- <artifactId>jboss-remoting</artifactId>
- <version>2.5.3</version>
- </dependency>
- <dependency>
- <groupId>concurrent</groupId>
- <artifactId>concurrent</artifactId>
- <version>1.3.4</version>
- </dependency>
- <dependency>
- <groupId>jboss</groupId>
- <artifactId>jboss-serialization</artifactId>
- <version>4.2.2.GA</version>
- </dependency>
服务端代码:
- import java.net.MalformedURLException;
- import javax.management.MBeanServer;
- import org.jboss.remoting.InvocationRequest;
- import org.jboss.remoting.InvokerLocator;
- import org.jboss.remoting.ServerInvocationHandler;
- import org.jboss.remoting.ServerInvoker;
- import org.jboss.remoting.callback.InvokerCallbackHandler;
- import org.jboss.remoting.invocation.NameBasedInvocation;
- import org.jboss.remoting.transport.Connector;
- public class ServerTest {
- public static void main(String[] args) {
- String locatorURI = "socket://127.0.0.1:8084";
- String params = "/?backlog=50&numAcceptThreads=30&maxPoolSize=30&clientMaxPoolSize=30&clientLeasePeriod=10000&timeout=8000";
- locatorURI += params;
- try {
- InvokerLocator locator = new InvokerLocator(locatorURI);
- Connector connector = new Connector(locator);
- connector.create();
- connector.addInvocationHandler("com.**.jhb.ServerHandle", new ServerInvocationHandler() {
- @Override
- public void addListener(InvokerCallbackHandler arg0) {
- }
- @Override
- public Object invoke(InvocationRequest arg0) throws Throwable {
- System.out.println("getRequestPayload " + arg0.getRequestPayload());
- // 方式一
- System.out.println("getParameter " + arg0.getParameter());
- // 方式二
- NameBasedInvocation obj = (NameBasedInvocation) arg0.getParameter();
- System.out.println("getParameter " + obj.getParameters()[0]);
- return "hello";
- }
- @Override
- public void removeListener(InvokerCallbackHandler arg0) {
- }
- @Override
- public void setInvoker(ServerInvoker arg0) {
- }
- @Override
- public void setMBeanServer(MBeanServer arg0) {
- }
- });
- connector.start();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public interface ServerHandle {
- String say(String hi);
- }
- import java.net.MalformedURLException;
- import java.util.HashMap;
- import org.jboss.remoting.Client;
- import org.jboss.remoting.InvokerLocator;
- public class ClientTest {
- public static void main(String[] args){
- String locatorURI = "socket://127.0.0.1:8084";
- String params = "/?backlog=50&numAcceptThreads=30&maxPoolSize=30&clientMaxPoolSize=30&clientLeasePeriod=10000&timeout=8000";
- locatorURI += params;
- try {
- InvokerLocator locator = new InvokerLocator(locatorURI);
- //方式1
- Client client = new Client(locator,ServerHandle.class.getName());
- client.connect();
- client.invoke("jhb ",new HashMap<String,String>());
- //方式二
- /*ServerHandle serverHandle=(ServerHandle) TransporterClient.createTransporterClient(locator, ServerHandle.class);
- serverHandle.say("jhb");*/
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- }
参考链接:
http://docs.jboss.org/jbossremoting/2.5.3.SP1/html/chapter-configuration.html#d0e1564