Introduced new module due to BAEL-598
This commit is contained in:
committed by
Victor Mosin
parent
5c85259243
commit
d8efbb5c75
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,55 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-thrift</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<java.versin>1.8</java.versin>
|
||||
<junit.version>4.12</junit.version>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<thrift.version>0.10.0</thrift.version>
|
||||
<maven-thrift.version>0.1.11</maven-thrift.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.thrift</groupId>
|
||||
<artifactId>libthrift</artifactId>
|
||||
<version>${thrift.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<defaultGoal>install</defaultGoal>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CrossPlatformServiceServer server = new CrossPlatformServiceServer();
|
||||
server.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
import org.apache.thrift.protocol.TBinaryProtocol;
|
||||
import org.apache.thrift.protocol.TProtocol;
|
||||
import org.apache.thrift.transport.TSocket;
|
||||
import org.apache.thrift.transport.TTransport;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
|
||||
public class CrossPlatformServiceClient {
|
||||
|
||||
public boolean ping() {
|
||||
try {
|
||||
TTransport transport;
|
||||
|
||||
transport = new TSocket("localhost", 9090);
|
||||
transport.open();
|
||||
|
||||
TProtocol protocol = new TBinaryProtocol(transport);
|
||||
CrossPlatformService.Client client = new CrossPlatformService.Client(protocol);
|
||||
|
||||
System.out.print("Calling remote method...");
|
||||
|
||||
boolean result = client.ping();
|
||||
|
||||
System.out.println("done.");
|
||||
|
||||
transport.close();
|
||||
|
||||
return result;
|
||||
} catch (TTransportException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TException x) {
|
||||
x.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformResource;
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
import com.baeldung.thrift.impl.InvalidOperationException;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CrossPlatformServiceImpl implements CrossPlatformService.Iface {
|
||||
|
||||
@Override
|
||||
public CrossPlatformResource get(final int id) throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
return new CrossPlatformResource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(final CrossPlatformResource resource) throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CrossPlatformResource> getList() throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ping() throws InvalidOperationException, TException {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
|
||||
import org.apache.thrift.server.TServer;
|
||||
import org.apache.thrift.server.TSimpleServer;
|
||||
import org.apache.thrift.transport.TServerSocket;
|
||||
import org.apache.thrift.transport.TServerTransport;
|
||||
|
||||
public class CrossPlatformServiceServer {
|
||||
|
||||
private TServer server;
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
TServerTransport serverTransport = new TServerSocket(9090);
|
||||
server = new TSimpleServer(new TServer.Args(serverTransport)
|
||||
.processor(new CrossPlatformService.Processor<>(new CrossPlatformServiceImpl())));
|
||||
|
||||
System.out.print("Starting the server... ");
|
||||
|
||||
server.serve();
|
||||
|
||||
System.out.println("done.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (server != null && server.isServing()) {
|
||||
System.out.print("Stopping the server... ");
|
||||
|
||||
server.stop();
|
||||
|
||||
System.out.println("done.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace cpp com.baeldung.thrift.impl
|
||||
namespace java com.baeldung.thrift.impl
|
||||
|
||||
exception InvalidOperationException {
|
||||
1: i32 code,
|
||||
2: string description
|
||||
}
|
||||
|
||||
struct CrossPlatformResource {
|
||||
1: i32 id,
|
||||
2: string name,
|
||||
3: optional string salutation
|
||||
}
|
||||
|
||||
service CrossPlatformService {
|
||||
|
||||
CrossPlatformResource get(1:i32 id) throws (1:InvalidOperationException e),
|
||||
|
||||
void save(1:CrossPlatformResource resource) throws (1:InvalidOperationException e),
|
||||
|
||||
list <CrossPlatformResource> getList() throws (1:InvalidOperationException e),
|
||||
|
||||
bool ping() throws (1:InvalidOperationException e)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CrossPlatformServiceTest {
|
||||
|
||||
private CrossPlatformServiceServer server = new CrossPlatformServiceServer();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
new Thread(() -> server.start()).start();
|
||||
try {
|
||||
// wait for the server start up
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ping() {
|
||||
CrossPlatformServiceClient client = new CrossPlatformServiceClient();
|
||||
Assert.assertTrue(client.ping());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user