Skip to content

Commit d8efbb5

Browse files
vicmosinVictor Mosin
authored andcommitted
Introduced new module due to BAEL-598
1 parent 5c85259 commit d8efbb5

11 files changed

Lines changed: 5033 additions & 0 deletions

File tree

apache-thrift/generated/com/baeldung/thrift/impl/CrossPlatformResource.java

Lines changed: 579 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apache-thrift/generated/com/baeldung/thrift/impl/CrossPlatformService.java

Lines changed: 3745 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apache-thrift/generated/com/baeldung/thrift/impl/InvalidOperationException.java

Lines changed: 472 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apache-thrift/pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-thrift</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>pom</packaging>
8+
9+
<properties>
10+
<java.versin>1.8</java.versin>
11+
<junit.version>4.12</junit.version>
12+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
13+
<thrift.version>0.10.0</thrift.version>
14+
<maven-thrift.version>0.1.11</maven-thrift.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.apache.thrift</groupId>
20+
<artifactId>libthrift</artifactId>
21+
<version>${thrift.version}</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>${junit.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-simple</artifactId>
34+
<version>1.7.12</version>
35+
<scope>test</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<defaultGoal>install</defaultGoal>
41+
<pluginManagement>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<version>${maven-compiler-plugin.version}</version>
47+
<configuration>
48+
<source>1.8</source>
49+
<target>1.8</target>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</pluginManagement>
54+
</build>
55+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.thrift;
2+
3+
public class Application {
4+
5+
public static void main(String[] args) {
6+
CrossPlatformServiceServer server = new CrossPlatformServiceServer();
7+
server.start();
8+
}
9+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.thrift;
2+
3+
import com.baeldung.thrift.impl.CrossPlatformService;
4+
5+
import org.apache.thrift.TException;
6+
import org.apache.thrift.protocol.TBinaryProtocol;
7+
import org.apache.thrift.protocol.TProtocol;
8+
import org.apache.thrift.transport.TSocket;
9+
import org.apache.thrift.transport.TTransport;
10+
import org.apache.thrift.transport.TTransportException;
11+
12+
public class CrossPlatformServiceClient {
13+
14+
public boolean ping() {
15+
try {
16+
TTransport transport;
17+
18+
transport = new TSocket("localhost", 9090);
19+
transport.open();
20+
21+
TProtocol protocol = new TBinaryProtocol(transport);
22+
CrossPlatformService.Client client = new CrossPlatformService.Client(protocol);
23+
24+
System.out.print("Calling remote method...");
25+
26+
boolean result = client.ping();
27+
28+
System.out.println("done.");
29+
30+
transport.close();
31+
32+
return result;
33+
} catch (TTransportException e) {
34+
e.printStackTrace();
35+
} catch (TException x) {
36+
x.printStackTrace();
37+
}
38+
39+
return false;
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.thrift;
2+
3+
import com.baeldung.thrift.impl.CrossPlatformResource;
4+
import com.baeldung.thrift.impl.CrossPlatformService;
5+
import com.baeldung.thrift.impl.InvalidOperationException;
6+
7+
import org.apache.thrift.TException;
8+
9+
import java.util.Collections;
10+
import java.util.List;
11+
12+
public class CrossPlatformServiceImpl implements CrossPlatformService.Iface {
13+
14+
@Override
15+
public CrossPlatformResource get(final int id) throws InvalidOperationException, TException {
16+
// add some action
17+
return new CrossPlatformResource();
18+
}
19+
20+
@Override
21+
public void save(final CrossPlatformResource resource) throws InvalidOperationException, TException {
22+
// add some action
23+
}
24+
25+
@Override
26+
public List<CrossPlatformResource> getList() throws InvalidOperationException, TException {
27+
// add some action
28+
return Collections.emptyList();
29+
}
30+
31+
@Override
32+
public boolean ping() throws InvalidOperationException, TException {
33+
return true;
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.thrift;
2+
3+
import com.baeldung.thrift.impl.CrossPlatformService;
4+
5+
import org.apache.thrift.server.TServer;
6+
import org.apache.thrift.server.TSimpleServer;
7+
import org.apache.thrift.transport.TServerSocket;
8+
import org.apache.thrift.transport.TServerTransport;
9+
10+
public class CrossPlatformServiceServer {
11+
12+
private TServer server;
13+
14+
public void start() {
15+
try {
16+
TServerTransport serverTransport = new TServerSocket(9090);
17+
server = new TSimpleServer(new TServer.Args(serverTransport)
18+
.processor(new CrossPlatformService.Processor<>(new CrossPlatformServiceImpl())));
19+
20+
System.out.print("Starting the server... ");
21+
22+
server.serve();
23+
24+
System.out.println("done.");
25+
} catch (Exception e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
public void stop() {
31+
if (server != null && server.isServing()) {
32+
System.out.print("Stopping the server... ");
33+
34+
server.stop();
35+
36+
System.out.println("done.");
37+
}
38+
}
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace cpp com.baeldung.thrift.impl
2+
namespace java com.baeldung.thrift.impl
3+
4+
exception InvalidOperationException {
5+
1: i32 code,
6+
2: string description
7+
}
8+
9+
struct CrossPlatformResource {
10+
1: i32 id,
11+
2: string name,
12+
3: optional string salutation
13+
}
14+
15+
service CrossPlatformService {
16+
17+
CrossPlatformResource get(1:i32 id) throws (1:InvalidOperationException e),
18+
19+
void save(1:CrossPlatformResource resource) throws (1:InvalidOperationException e),
20+
21+
list <CrossPlatformResource> getList() throws (1:InvalidOperationException e),
22+
23+
bool ping() throws (1:InvalidOperationException e)
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.thrift;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class CrossPlatformServiceTest {
9+
10+
private CrossPlatformServiceServer server = new CrossPlatformServiceServer();
11+
12+
@Before
13+
public void setUp() {
14+
new Thread(() -> server.start()).start();
15+
try {
16+
// wait for the server start up
17+
Thread.sleep(1000);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
23+
@After
24+
public void tearDown() {
25+
server.stop();
26+
}
27+
28+
@Test
29+
public void ping() {
30+
CrossPlatformServiceClient client = new CrossPlatformServiceClient();
31+
Assert.assertTrue(client.ping());
32+
}
33+
}

0 commit comments

Comments
 (0)