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);
};
};
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);
}
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());
}
}
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
Execute mvn package in the project root directory to generate war package, which can be published by the management system later.
<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
```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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。