Introduction to developing zeroc ice applications (java develops ice applications, Python develops ice applications, and Java develops ice services with python)

Posted by emrys404 on Mon, 26 Aug 2019 12:03:20 +0200

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

Install ice

1. Download Address of Official Website https://zeroc.com/downloads/ice

2. Installation program, this article installs location E: Program Files ZeroC Ice-3.6.3

3. Configuring environment variables

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

1) Create a new ICE_HOME with the variable value of installation path

2) Add "% ICE_HOME%bin" to Path“

3) Inspection

Open cmd after configuration is complete

The installation and configuration is completed when the version number appears.

 

 

 

java combined with ice development

Create a maven-managed java project

Adding dependencies to pom.xml

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

Dependency best corresponds to ice version

A Fast Way to Find Dependency

For example, maven ice

 

 

Create a slice folder under the project folder. Create a file ** ice.

Examples of this project Hello.ice

[["java:package:com.test.ice.service"]] // Define the java package name parent structure
module demo   //Module package name
{
	interface Hello   //Interface Service Name
	{
		string sayHello(string s);   //Specific methods
	};
};

 

1) Use slice2java to compile ice file to generate java code

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

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

If you can't see the code, refresh the project

2) Generate code using eclipse plug-ins

eclipse-Help-Marketplace search ice, the first plug-in install

Restart eclipse after installation

After reboot, right-click on the project name and select Ice builder - > add Ice builder

Generate ice's Java code

 

Programming

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

1) The interface implementation class HelloImpl inherits _HelloDisp.java

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, it can be neglected. It's better to pack the interface project into jar and install it into the local library in maven way, which is not the same project as the interface, that is, the api, the interface project, the server project, the client project. https://www.jianshu.com/p/5ce9d1567fee Adding interface project dependencies to other projects

2) Service startup class Server

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) Running procedures

First run the service startup class Server,

Start client Client

Server receives data

The client receives the data returned by the server

Simple java combined with ice development to the end of this introduction, specific in-depth follow-up work together oh

 

 

python develops ice project

1. Dependency packages required to install ice

pip install zeroc-ice

2. Writing ice documents

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

3. Write Server Startup Program

#!/usr/bin/env python
# coding=utf-8
import sys, Ice

# Dynamic loading slice file and compiling
Ice.loadSlice("./demo.ice")

#Module name in ice file
import demo
## Implementing a service class
class HelloImpl(demo.Hello):
    def sayHello(self, s, current=None):
        print s
        msg ="Hello,"+s
        return msg


with Ice.initialize(sys.argv) as communicator:
    print "Server starting..."
    adapter = communicator.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10006")
    object = HelloImpl()
    adapter.add(object, communicator.stringToIdentity("hello"))
    adapter.activate()
    print "Server start success."
    communicator.waitForShutdown()

4. Write client startup program

#!/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. Running program

1) Start the server program

2) Start client program

3) server receives data from client and outputs it

 

4) client receives the data returned by server and outputs it

 

 

 

java and python services call each other

java starts server service and python client program calls service

1) Start the java server Service

2) Start the python client service

3) server service receives data and outputs it

4) client receives the returned data and outputs it

 

So far, just look outside the door.

Topics: Java Python Maven Eclipse