forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced new module due to BAEL-598
- Loading branch information
Showing
11 changed files
with
5,033 additions
and
0 deletions.
There are no files selected for viewing
579 changes: 579 additions & 0 deletions
579
apache-thrift/generated/com/baeldung/thrift/impl/CrossPlatformResource.java
Large diffs are not rendered by default.
Oops, something went wrong.
3,745 changes: 3,745 additions & 0 deletions
3,745
apache-thrift/generated/com/baeldung/thrift/impl/CrossPlatformService.java
Large diffs are not rendered by default.
Oops, something went wrong.
472 changes: 472 additions & 0 deletions
472
apache-thrift/generated/com/baeldung/thrift/impl/InvalidOperationException.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
9 changes: 9 additions & 0 deletions
9
apache-thrift/src/main/java/com/baeldung/thrift/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.thrift; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
CrossPlatformServiceServer server = new CrossPlatformServiceServer(); | ||
server.start(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
apache-thrift/src/main/java/com/baeldung/thrift/CrossPlatformServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
apache-thrift/src/main/java/com/baeldung/thrift/CrossPlatformServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
apache-thrift/src/main/java/com/baeldung/thrift/CrossPlatformServiceServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
apache-thrift/src/main/resources/cross-platform-service.thrift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
33 changes: 33 additions & 0 deletions
33
apache-thrift/src/test/java/com/baeldung/thrift/CrossPlatformServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters