Getting zeroc ice application development (java ice application development, python ice application development, java and python binding develop ice service)

 

 rpc ice as a framework for the design mainstream platforms, including Windows and Linux, supports a wide range of languages, including C ++, Java, C # (and other .Net languages ​​such as Visual Basic), Python, Ruby, PHP and ActionScript.

Installation ice   

1. Download the official website address   https://zeroc.com/downloads/ice

2. Setup herein installation location E: \ Program Files \ ZeroC \ Ice-3.6.3

3. Set Environment Variables

Computer -> Properties -> Advanced System Settings -> Environment Variables

1) to establish a new ICE_HOME, variable is the installation path

2) Add "% ICE_HOME% \ bin" in the Path

3) test

After configuration is complete, open cmd

The version number is the emergence of the installation and configuration is complete

 

 

 

java development combined with ice

Create a maven java project management

Add depend in pom.xml

<!-- https://mvnrepository.com/artifact/com.zeroc/ice -->
		<dependency>
			<groupId>com.zeroc</groupId>
			<artifactId>ice</artifactId>
			<version>3.6.3</version>
		</dependency>

Preferably corresponds to version-dependent ice

Quickly find methods rely directly Baidu maven + more than the required dependencies name

As maven ice

 

 

Create a slice folder create a file in the project folder **. Ice

The project example Hello.ice

[[ "java: package: com.test.ice.service "]] // java package names defined in the parent structure 
module demo // module package names 
{ 
	interface the Hello interface service name // 
	{ 
		String the sayHello (String S); // specific methods 
	}; 
};

 

1) Use slice2java ice compiled file generating java code

Open the folder where the file in the cmd shell or ice, use the following command ps: - output-dir directory output file

slice2java .\Hello.ice --output-dir ..\src\main\java

If you do not see the code, you can refresh the project

2) using the generated code plug eclipse

eclipse - Help - Marketplace search for ice, first install a plug-in 

After the installation is complete restart eclipse

 After the restart, select the name of the project right select Ice builder -> add ice builder

The ice is generated Java code

 

Programming

In the generated code can be seen in "_ice file interface name Disp.java" file, for example, this project is _HelloDisp.java

1) interface class inherits _HelloDisp.java HelloImpl

import com.test.ice.service.demo._HelloDisp;

import Ice.Current;

public class HelloImpl extends _HelloDisp{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String sayHello(String s, Current __current) {
		System.out.println(s);
		return "hello,"+s;
	}
	
}

ps: next words can be ignored, it is best not interface with the same project, ie the interface is api project, server project, client projects, interface project maven way to install packaged into a jar to the local library, a specific operation example https: //www.jianshu.com/p/5ce9d1567fee , other items add an interface project dependencies

2) Class Server service starts

import com.test.ice.service.demo.impl.HelloImpl;

import Ice.Communicator;
import Ice.ObjectAdapter;

public class Server {
 
	public static void main(String[] args) {
		int status = 0;
		Communicator ic = null;
		try{
			System.out.println("Server starting...");
			ic = Ice.Util.initialize(args);
			ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("iceTest", "default -p 10006");
			Ice.Object object = new HelloImpl();
			adapter.add(object, ic.stringToIdentity("hello"));
			adapter.activate();
			System.out.println("Server start success.");
			ic.waitForShutdown();
		}catch(Ice.LocalException e){
			e.printStackTrace();
			status = 1;
		}catch(Exception e){
			System.err.println(e.getMessage());
			status = 1;
		}
		if(ic != null){
			try{
				ic.destroy();
			}catch(Exception e){
				System.err.println(e.getMessage());
				status = 1;
			}
		}
		System.exit(status);
	}
 
}

3) Client client

import com.test.ice.service.demo.HelloPrx;
import com.test.ice.service.demo.HelloPrxHelper;

public class Client {

	public static void main(String[] args) {
		int status = 0;
		Ice.Communicator ic = null;
		try {
			ic = Ice.Util.initialize(args);
			Ice.ObjectPrx base = ic.stringToProxy("hello:default -p 10006");
			HelloPrx hello = HelloPrxHelper.checkedCast(base);
			if (hello == null) {
				throw new Error("Invalid proxy");
			}

			String s = hello.sayHello("World!");
			System.out.println(">>" + s);		
		} catch (Ice.LocalException e) {
			e.printStackTrace();
			status = 1;
		} catch (Exception e) {
			System.err.println(e.getMessage());
			status = 1;
		}
		if (ic != null) {
			try {
				ic.destroy();
			} catch (Exception e) {
				System.err.println(e.getMessage());
				status = 1;
			}
		}
		System.exit(status);
	}
}

4) Run the program 

First class service starts running Server,

Start the client Client

Termination by the data service

Client receives the data returned from the server

Simple java introduce the end of this combination of ice development, specific in-depth follow-up efforts with oh

 

 

python project development ice

 1. Install ice required dependencies

pip install zeroc-ice

 2. Write ice file

module demo
{
	interface Hello
	{
		string sayHello(string s);
	};
};

3. Write Server Launcher

#! / usr / bin / env Python 
# Coding = UTF-8 
Import SYS, Ice 

# dynamically loaded slice file and compile 
Ice.loadSlice ( "./ demo.ice") 

module name #ice file 
Import Demo 
## achieve a service class 
class the HelloImpl (demo.Hello): 
    DEF sayHello (Self, S, Current = None): 
        Print S 
        msg = "the Hello," S + 
        return msg 


with Ice.initialize (sys.argv) AS Communicator: 
    Print "Server ... Starting " 
    Adapter = communicator.createObjectAdapterWithEndpoints (" SimplePrinterAdapter "," default -p 10006 ") 
    Object = the HelloImpl () 
    adapter.add (Object, communicator.stringToIdentity (" Hello ")) 
    adapter.activate () 
    Print"Server start success." 
    Communicator.waitForShutdown()

4. Write the client program to start

#!/usr/bin/env python
# coding=utf-8
import sys, Ice
Ice.loadSlice("./demo.ice")
import demo

with Ice.initialize(sys.argv) as communicator:
    base = communicator.stringToProxy("hello:default -p 10006")
    printer = demo.HelloPrx.checkedCast(base)
    if not printer:
        raise RuntimeError("Invalid proxy")
    print  printer.sayHello("World!")

5. Run the program   

1) Start server program

2) start the client program

3) server receives client data transmitted and outputs

 

4) client server returns received data and outputs

 

 

 

 java and python service call each other

java start server service, python client program calls the service

1) Start java server service

2) start the python client service

3) server receives service data and outputs

4) client receives the returned data and outputs

 

 So far, the door to see it

 

Guess you like

Origin www.cnblogs.com/sangewuxie/p/11412587.html