Skip to the content.

TarsJava

1 Define interface file

The definition of interface file is defined by the tars interface description language. Create the hello.tars file in the src/main/resources directory. The content is as follows:

module TestApp 
{
	interface Hello
	{
	    string hello(int no, string name);
	};
};

2 Interface file compilation

Provide plugins to compile and generate java code, add and generate java file configuration in tars-maven-plugin:

<plugin>
	<groupId>com.tencent.tars</groupId>
	<artifactId>tars-maven-plugin</artifactId>
	<version>1.6.1</version>
	<configuration>
		<tars2JavaConfig>
			<!-- tars file locate-->
			<tarsFiles>
				<tarsFile>${basedir}/src/main/resources/hello.tars</tarsFile>
			</tarsFiles>
			<!-- source encoding-->
			<tarsFileCharset>UTF-8</tarsFileCharset>
			<!-- true:create server source code, false: create client source code-->
			<servant>true</servant>
			<!-- generated source file encoding-->
			<charset>UTF-8</charset>
			<!-- generated source file locate -->
			<srcPath>${basedir}/src/main/java</srcPath>
			<!-- package prefix -->
			<packagePrefixName>com.qq.tars.quickstart.server.</packagePrefixName>
		</tars2JavaConfig>
	</configuration>
</plugin>

Execute mvn tars:tars2java in the project root directory. The HelloServant.java will be created and filled with the following lines of code automatically.

@Servant
public interface HelloServant {

	public String hello(int no, String name);
}	

3 Service interface implementation

create HelloServantImpl.java, Implement HelloServant.java interface

public class HelloServantImpl implements HelloServant {

    @Override
    public String hello(int no, String name) {
        return String.format("hello no=%s, name=%s, time=%s", no, name, System.currentTimeMillis());
    }
}

4 Service exposure configuration

Create a services.xml configuration file under resources dir. After the service is written, it needs to load the configuration exposure service when the process starts. The configuration is as follows

<?xml version="1.0" encoding="UTF-8"?>
<servants>
	<servant name="HelloObj">
		<home-api>com.qq.tars.quickstart.server.testapp.HelloServant</home-api>
		<home-class>com.qq.tars.quickstart.server.testapp.impl.HelloServantImpl</home-class>
	</servant>
</servants>

in addition to this method, you can also use spring mode to configure services

5 Service compilation and packaging

Execute mvn package in the project root directory to generate war package, which can be published by the management system later.

6 RPC Call

<dependency>
	<groupId>com.tencent.tars</groupId>
   	<artifactId>tars-client</artifactId>
   	<version>1.6.1</version>
   	<type>jar</type>
</dependency>    
- write code according to service tar interface file
​```java
@Servant
public interface HelloPrx {
      
	public String hello(int no, String name);
      
      	public String hello(int no, String name, @TarsContext java.util.Map<String, String> ctx);
      
      	public void async_hello(@TarsCallback HelloPrxCallback callback, int no, String name);
      
      	public void async_hello(@TarsCallback HelloPrxCallback callback, int no, String name, @TarsContext java.util.Map<String, String> ctx);
}
public static void main(String[] args) {
	CommunicatorConfig cfg = new CommunicatorConfig();
        //create Communicator
        Communicator communicator = CommunicatorFactory.getInstance().getCommunicator(cfg);
        //create proxy by Communicator
        HelloPrx proxy = communicator.stringToProxy(HelloPrx.class, "TestApp.HelloServer.HelloObj");
        String ret = proxy.hello(1000, "HelloWorld");
        System.out.println(ret);
}
public static void main(String[] args) {
	CommunicatorConfig cfg = new CommunicatorConfig();
        //create Communicator
        Communicator communicator = CommunicatorFactory.getInstance().getCommunicator(cfg);
        //create proxy by Communicator
        HelloPrx proxy = communicator.stringToProxy(HelloPrx.class, "TestApp.HelloServer.HelloObj");
        proxy.async_hello(new HelloPrxCallback() {
        		
        	@Override
        	public void callback_expired() {
        	}
        		
        	@Override
        	public void callback_exception(Throwable ex) {
        	}
        		
        	@Override
        	public void callback_hello(String ret) {
        		System.out.println(ret);
        	}
        }, 1000, "HelloWorld");
}

Communicator manages the client’s resources, preferably globally object