java corba 简单例子测试客户端多连接

1.
定义hello.idl
module hello{ 
interface Hello{ 
string say(); 
}; 
}; 

2.
command > idlj -f all hello.idl
生成如下文件
HelloPOA POA指的是Portable Object Adapter(轻便对象适配器)。这个抽象类是一个基于流的服务器端骨架,提供了服务器端基本的CORBA功能。
_HelloSutb 客户端的存根类,为客户端提供了CORBA功能。
Hello 这是java版的Hello接口,它提供了标准的CORBA对象功能。
HelloHelper 这是一个辅助类,负责向CORBA流中写入或读取对象。
HelloHolder 这是一个final类,它持有一个public的Hello实例变量。它用来操作CORBA输入输出流的参数。
HelloOperations 这个类才是我们所预想的那个接口,只包含我们定义的那个方法,不包含CORBA的任何东西

3.
HelloImpl.java
package demo.corba.hello;

import java.util.concurrent.atomic.AtomicInteger;

public class HelloImpl extends HelloPOA {

	private AtomicInteger counter = new AtomicInteger(0);
	
	@Override
	public String say() {
		// 模拟实际请求需要500ms
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
                //返回当前请求的序号
		return "responseid_"+counter.incrementAndGet();
	}

}


4.
HelloServer.java
package demo.corba.hello;

import java.util.Properties;  

import org.omg.CORBA.ORB;  
import org.omg.CosNaming.NameComponent;  
import org.omg.CosNaming.NamingContextExt;  
import org.omg.CosNaming.NamingContextExtHelper;  
import org.omg.PortableServer.POA;  
import org.omg.PortableServer.POAHelper;  
  
import demo.corba.hello.Hello;  
import demo.corba.hello.HelloHelper;  
  
public class HelloServer {  
    public void run(String[] args) throws Exception {  
        
        Properties props = new Properties();  
         
        props.put("org.omg.CORBA.ORBInitialPort", "1050");  
         
        props.put("org.omg.CORBA.ORBInitialHost", "localhost");  
        
        ORB orb = ORB.init(args, props);  
         
        
        POA poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));  
        poa.the_POAManager().activate();  
         
        HelloImpl hello = new HelloImpl();  
          
        org.omg.CORBA.Object ref = poa.servant_to_reference(hello);  
        Hello href = HelloHelper.narrow(ref);  
        
        org.omg.CORBA.Object objref = orb.resolve_initial_references("NameService");  
          
        NamingContextExt ncRef = NamingContextExtHelper.narrow(objref);  
        
        String name = "Hello";  
        NameComponent[] nc = ncRef.to_name(name);  
        ncRef.rebind(nc, href);  
        System.out.println("HelloServer ready and listening......");  
        //运行ORB  
        orb.run();  
    }  
      
    public static void main(String[] args) {  
        HelloServer server = new HelloServer();  
        try {  
            server.run(args);  
        }catch(Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  



5.
HelloClient.java
package demo.corba.hello;

import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;

import demo.corba.hello.Hello;
import demo.corba.hello.HelloHelper;

public class HelloClient {

	private static AtomicInteger reqCounter = new AtomicInteger(0);
	private static StringBuffer sb = new StringBuffer();

	private static Hello instance = getHello();
	//每个线程创建一个Hello对象
	private static ThreadLocal<Hello> instances = new ThreadLocal<Hello>(){

		@Override
		protected Hello initialValue() {
			return getHello();
		}
		
	};

	private static Hello getHello() {
		try {
			Properties props = new Properties();
			
			props.put("org.omg.CORBA.ORBInitialPort", "1050");
			props.put("org.omg.CORBA.ORBInitialHost", "localhost");
			ORB orb = ORB.init(new String[0], props);
			
			org.omg.CORBA.Object objRef = orb
					.resolve_initial_references("NameService");
			
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			
			String name = "Hello";
			Hello hello = HelloHelper.narrow(ncRef.resolve_str(name));
			return hello;
		} catch (Exception e) {
			return null;
		}

	}

	public static String callHello() {
		
//		String result = instance.say();
		String result = instances.get().say();
		System.out.println(result);
		return result;
	}

	private static class Task implements Runnable {

		private AtomicInteger counter = new AtomicInteger(0);

		@Override
		public void run() {
                        //每个线程请求20次,每次累积所有线程的总请求数
			while (true) {
				int number = counter.incrementAndGet();
				if (number > 20) {
					return;
				}
				try {
					String response = callHello();
					String item = String
							.format("threadid:%s,response:%s,requestCount:%s,threadcount:%s",
									Thread.currentThread().getId(), response,
									reqCounter.incrementAndGet(), number);
					System.out.println(item);
					sb.append(item).append("\n");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	public static void main(String[] args) {
		try {
			Thread[] threads = new Thread[50];
			for (int i = 0; i < threads.length; i++) {
				threads[i] = new Thread(new Task());
			}
			for (int i = 0; i < threads.length; i++) {
				threads[i].start();
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
		try {
			Thread.sleep(60 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(sb.toString());

	}
}


6.
run corba service
orbd -port 1050 -ORBInitialPort 1049 -ORBInitialHost localhost
java HelloServer
java HelloClient

7.
check corba connection number
netstat -aon|findstr 1050 (windows)

你可能感兴趣的:(corba)