Skip to content
Snippets Groups Projects
Commit c8809b1d authored by nmittler's avatar nmittler Committed by Tim Emiola
Browse files

Removing java examples from grpc-common

parent 94e38f6b
No related branches found
No related tags found
No related merge requests found
<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>
<parent>
<groupId>com.google.net.stubby</groupId>
<artifactId>stubby-parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<artifactId>grpc-hello-world</artifactId>
<packaging>jar</packaging>
<name>Hello gRPC World</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>stubby-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>stubby-netty</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>stubby-okhttp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>stubby-stub</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>stubby-testing</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>com.internetitem</groupId>
<artifactId>write-properties-file-maven-plugin</artifactId>
<executions>
<execution>
<id>bootclasspath</id>
<phase>prepare-package</phase>
<goals>
<goal>write-properties-file</goal>
</goals>
<configuration>
<filename>bootclasspath.properties</filename>
<outputDirectory>${project.build.directory}</outputDirectory>
<properties>
<property>
<name>bootclasspath</name>
<value>${argLine.bootcp}</value>
</property>
<property>
<name>jar</name>
<value>${project.build.directory}/${project.artifactId}-${project.version}-jar-with-dependencies.jar</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
#!/bin/bash -e
TARGET='Greeter Client'
TARGET_CLASS='ex.grpc.GreeterClient'
TARGET_ARGS="$@"
cd "$(dirname "$0")"
mvn -q -nsu -am package -Dcheckstyle.skip=true -DskipTests
. target/bootclasspath.properties
echo "[INFO] Running: $TARGET ($TARGET_CLASS $TARGET_ARGS)"
exec java "$bootclasspath" -cp "$jar" "$TARGET_CLASS" $TARGET_ARGS
#!/bin/bash -e
TARGET='Greeter Server'
TARGET_CLASS='ex.grpc.GreeterServer'
cd "$(dirname "$0")"
mvn -q -nsu -am package -Dcheckstyle.skip=true -DskipTests
. target/bootclasspath.properties
echo "[INFO] Running: $TARGET ($TARGET_CLASS)"
exec java "$bootclasspath" -cp "$jar" "$TARGET_CLASS"
package ex.grpc;
import com.google.net.stubby.ChannelImpl;
import com.google.net.stubby.stub.StreamObserver;
import com.google.net.stubby.transport.netty.NegotiationType;
import com.google.net.stubby.transport.netty.NettyChannelBuilder;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.concurrent.TimeUnit;
public class GreeterClient {
private final Logger logger = Logger.getLogger(
GreeterClient.class.getName());
private final ChannelImpl channel;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
public GreeterClient(String host, int port) {
channel = NettyChannelBuilder.forAddress(host, port)
.negotiationType(NegotiationType.PLAINTEXT)
.build();
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTerminated(5, TimeUnit.SECONDS);
}
public void greet(String name) {
try {
logger.fine("Will try to greet " + name + " ...");
Helloworld.HelloRequest req =
Helloworld.HelloRequest.newBuilder().setName(name).build();
Helloworld.HelloReply reply = blockingStub.sayHello(req);
logger.info("Greeting: " + reply.getMessage());
} catch (RuntimeException e) {
logger.log(Level.WARNING, "RPC failed", e);
return;
}
}
public static void main(String[] args) throws Exception {
GreeterClient client = new GreeterClient("localhost", 50051);
try {
/* Access a service running on the local machine on port 50051 */
String user = "world";
if (args.length > 0) {
user = args[0]; /* Use the arg as the name to greet if provided */
}
client.greet(user);
} finally {
client.shutdown();
}
}
}
package ex.grpc;
import static com.google.net.stubby.stub.Calls.createMethodDescriptor;
import static com.google.net.stubby.stub.Calls.asyncUnaryCall;
import static com.google.net.stubby.stub.Calls.asyncServerStreamingCall;
import static com.google.net.stubby.stub.Calls.asyncClientStreamingCall;
import static com.google.net.stubby.stub.Calls.duplexStreamingCall;
import static com.google.net.stubby.stub.Calls.blockingUnaryCall;
import static com.google.net.stubby.stub.Calls.blockingServerStreamingCall;
import static com.google.net.stubby.stub.Calls.unaryFutureCall;
import static com.google.net.stubby.stub.ServerCalls.createMethodDefinition;
import static com.google.net.stubby.stub.ServerCalls.asyncUnaryRequestCall;
import static com.google.net.stubby.stub.ServerCalls.asyncStreamingRequestCall;
@javax.annotation.Generated("by gRPC proto compiler")
public class GreeterGrpc {
private static final com.google.net.stubby.stub.Method<ex.grpc.Helloworld.HelloRequest,
ex.grpc.Helloworld.HelloReply> METHOD_SAY_HELLO =
com.google.net.stubby.stub.Method.create(
com.google.net.stubby.MethodType.UNARY, "sayHello",
com.google.net.stubby.proto.ProtoUtils.marshaller(ex.grpc.Helloworld.HelloRequest.PARSER),
com.google.net.stubby.proto.ProtoUtils.marshaller(ex.grpc.Helloworld.HelloReply.PARSER));
public static GreeterStub newStub(com.google.net.stubby.Channel channel) {
return new GreeterStub(channel, CONFIG);
}
public static GreeterBlockingStub newBlockingStub(
com.google.net.stubby.Channel channel) {
return new GreeterBlockingStub(channel, CONFIG);
}
public static GreeterFutureStub newFutureStub(
com.google.net.stubby.Channel channel) {
return new GreeterFutureStub(channel, CONFIG);
}
public static final GreeterServiceDescriptor CONFIG =
new GreeterServiceDescriptor();
@javax.annotation.concurrent.Immutable
public static class GreeterServiceDescriptor extends
com.google.net.stubby.stub.AbstractServiceDescriptor<GreeterServiceDescriptor> {
public final com.google.net.stubby.MethodDescriptor<ex.grpc.Helloworld.HelloRequest,
ex.grpc.Helloworld.HelloReply> sayHello;
private GreeterServiceDescriptor() {
sayHello = createMethodDescriptor(
"helloworld.Greeter", METHOD_SAY_HELLO);
}
private GreeterServiceDescriptor(
java.util.Map<java.lang.String, com.google.net.stubby.MethodDescriptor<?, ?>> methodMap) {
sayHello = (com.google.net.stubby.MethodDescriptor<ex.grpc.Helloworld.HelloRequest,
ex.grpc.Helloworld.HelloReply>) methodMap.get(
CONFIG.sayHello.getName());
}
@java.lang.Override
protected GreeterServiceDescriptor build(
java.util.Map<java.lang.String, com.google.net.stubby.MethodDescriptor<?, ?>> methodMap) {
return new GreeterServiceDescriptor(methodMap);
}
@java.lang.Override
public com.google.common.collect.ImmutableList<com.google.net.stubby.MethodDescriptor<?, ?>> methods() {
return com.google.common.collect.ImmutableList.<com.google.net.stubby.MethodDescriptor<?, ?>>of(
sayHello);
}
}
public static interface Greeter {
public void sayHello(ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply> responseObserver);
}
public static interface GreeterBlockingClient {
public ex.grpc.Helloworld.HelloReply sayHello(ex.grpc.Helloworld.HelloRequest request);
}
public static interface GreeterFutureClient {
public com.google.common.util.concurrent.ListenableFuture<ex.grpc.Helloworld.HelloReply> sayHello(
ex.grpc.Helloworld.HelloRequest request);
}
public static class GreeterStub extends
com.google.net.stubby.stub.AbstractStub<GreeterStub, GreeterServiceDescriptor>
implements Greeter {
private GreeterStub(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreeterStub build(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterStub(channel, config);
}
@java.lang.Override
public void sayHello(ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply> responseObserver) {
asyncUnaryCall(
channel.newCall(config.sayHello), request, responseObserver);
}
}
public static class GreeterBlockingStub extends
com.google.net.stubby.stub.AbstractStub<GreeterBlockingStub, GreeterServiceDescriptor>
implements GreeterBlockingClient {
private GreeterBlockingStub(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreeterBlockingStub build(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterBlockingStub(channel, config);
}
@java.lang.Override
public ex.grpc.Helloworld.HelloReply sayHello(ex.grpc.Helloworld.HelloRequest request) {
return blockingUnaryCall(
channel.newCall(config.sayHello), request);
}
}
public static class GreeterFutureStub extends
com.google.net.stubby.stub.AbstractStub<GreeterFutureStub, GreeterServiceDescriptor>
implements GreeterFutureClient {
private GreeterFutureStub(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreeterFutureStub build(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterFutureStub(channel, config);
}
@java.lang.Override
public com.google.common.util.concurrent.ListenableFuture<ex.grpc.Helloworld.HelloReply> sayHello(
ex.grpc.Helloworld.HelloRequest request) {
return unaryFutureCall(
channel.newCall(config.sayHello), request);
}
}
public static com.google.net.stubby.ServerServiceDefinition bindService(
final Greeter serviceImpl) {
return com.google.net.stubby.ServerServiceDefinition.builder("helloworld.Greeter")
.addMethod(createMethodDefinition(
METHOD_SAY_HELLO,
asyncUnaryRequestCall(
new com.google.net.stubby.stub.ServerCalls.UnaryRequestMethod<
ex.grpc.Helloworld.HelloRequest,
ex.grpc.Helloworld.HelloReply>() {
@java.lang.Override
public void invoke(
ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply> responseObserver) {
serviceImpl.sayHello(request, responseObserver);
}
}))).build();
}
}
package ex.grpc;
import com.google.net.stubby.stub.StreamObserver;
public class GreeterImpl implements GreeterGrpc.Greeter {
@Override
public void sayHello(Helloworld.HelloRequest req,
StreamObserver<Helloworld.HelloReply> responseObserver) {
Helloworld.HelloReply reply = Helloworld.HelloReply.newBuilder().setMessage(
"Hello " + req.getName()).build();
responseObserver.onValue(reply);
responseObserver.onCompleted();
}
}
package ex.grpc;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.net.stubby.ServerImpl;
import com.google.net.stubby.transport.netty.NettyServerBuilder;
import java.util.concurrent.TimeUnit;
/**
* Server that manages startup/shutdown of a {@code Greeter} server.
*/
public class GreeterServer {
/* The port on which the server should run */
private int port = 50051;
private ServerImpl server;
private void start() throws Exception {
server = NettyServerBuilder.forPort(port)
.addService(GreeterGrpc.bindService(new GreeterImpl()))
.build();
server.startAsync();
server.awaitRunning(5, TimeUnit.SECONDS);
System.out.println("Server started on port:" + port);
}
private void stop() throws Exception {
server.stopAsync();
server.awaitTerminated();
System.out.println("Server shutting down ...");
}
/**
* Main launches the server from the command line.
*/
public static void main(String[] args) throws Exception {
final GreeterServer server = new GreeterServer();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
System.out.println("Shutting down");
server.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
});
server.start();
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment