Table of contents
Directory
Updated at 2020-03-20 16:00:22

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 in the project root directory: tars:tars2java

@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

  • Create client project
  • Add dependency
<dependency>
	<groupId>com.tencent.tars</groupId>
   	<artifactId>tars-client</artifactId>
   	<version>1.6.1</version>
   	<type>jar</type>
</dependency>    
  • Add plugins

    <plugin>
     	<groupId>com.tencent.tars</groupId>
     	<artifactId>tars-maven-plugin</artifactId>
     	<version>1.6.1</version>
     	<configuration>
     		<tars2JavaConfig>
     			<tarsFiles>
     				<tarsFile>${basedir}/src/main/resources/hello.tars</tarsFile>
     			</tarsFiles>
     			<tarsFileCharset>UTF-8</tarsFileCharset>
     			<!-- create source code,PS: Client call, must be false here -->
     			<servant>false</servant>
     			<charset>UTF-8</charset>
     			<srcPath>${basedir}/src/main/java</srcPath>
     			<packagePrefixName>com.qq.tars.quickstart.client.</packagePrefixName>
     		</tars2JavaConfig>
     	</configuration>
    </plugin>
    
- write code according to service tar interface file

@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);

}


* Synchronous invocation

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);

}


* Asynchronous call

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**



Chapter