Skip to content

Commit

Permalink
Introduced new module due to BAEL-598
Browse files Browse the repository at this point in the history
  • Loading branch information
vicmosin authored and Victor Mosin committed Feb 1, 2017
1 parent 5c85259 commit d8efbb5
Show file tree
Hide file tree
Showing 11 changed files with 5,033 additions and 0 deletions.

Large diffs are not rendered by default.

3,745 changes: 3,745 additions & 0 deletions apache-thrift/generated/com/baeldung/thrift/impl/CrossPlatformService.java

Large diffs are not rendered by default.

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions apache-thrift/pom.xml
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>
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();
}
}
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;
}
}
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;
}
}
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 apache-thrift/src/main/resources/cross-platform-service.thrift
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)
}
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());
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>apache-cxf</module>
<module>apache-fop</module>
<module>apache-poi</module>
<module>apache-thrift</module>
<module>aspectj</module>
<module>assertj</module>
<module>autovalue</module>
Expand Down

0 comments on commit d8efbb5

Please sign in to comment.