Skip to content

Commit

Permalink
Chapter 5 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kenfinnigan committed Nov 11, 2016
1 parent 851f37d commit b4ae6b9
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 1 deletion.
2 changes: 1 addition & 1 deletion chapter4/resteasy-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.14.Final</version>
<version>3.0.19.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
46 changes: 46 additions & 0 deletions chapter5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>ejm</groupId>
<artifactId>ejm-samples-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>chapter5</artifactId>
<name>Chapter 5: Discovering Microservices for Consumption examples</name>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>topology-jgroups</artifactId>
</dependency>
</dependencies>

<modules>
<module>time</module>
<module>ribbon-client</module>
<module>resteasy-client</module>
</modules>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<properties>
<swarm.bind.address>127.0.0.1</swarm.bind.address>
<java.net.preferIPv4Stack>true</java.net.preferIPv4Stack>
</properties>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
52 changes: 52 additions & 0 deletions chapter5/resteasy-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>ejm</groupId>
<artifactId>chapter5</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>chapter5-resteasy-client</artifactId>
<name>Chapter 5: RESTEasy Client</name>
<packaging>war</packaging>

<build>
<finalName>resteasyclient</finalName>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>topology</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.19.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ejm.chapter5.resteasyclient;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.enterprise.concurrent.ManagedExecutorService;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.wildfly.swarm.topology.Topology;

/**
* @author Ken Finnigan
*/
@Path("/")
public class MessageResource {

private Topology topology;

public MessageResource() {
try {
topology = Topology.lookup();
} catch (NamingException e) {
e.printStackTrace();
}
}

@GET
@Path("/sync")
public String getMessageSync() throws Exception {
ResteasyClient client = new ResteasyClientBuilder().build();
URI url = getService("time");
ResteasyWebTarget target = client.target(url);

TimeService timeService = target.proxy(TimeService.class);
String time = timeService.getTime();

return message(time, url.toString());
}

@GET
@Path("/async")
public void getMessageAsync(@Suspended final AsyncResponse asyncResponse) throws Exception {
executorService().execute(() -> {
ResteasyClient client = new ResteasyClientBuilder().build();
URI url = null;
try {
url = getService("time");
} catch (Exception e) {
asyncResponse.resume(e);
}

ResteasyWebTarget target = client.target(url);

TimeService timeService = target.proxy(TimeService.class);
String time = timeService.getTime();

asyncResponse.resume(message(time, url.toString()));
});
}

private URI getService(String name) throws Exception {
Map<String, List<Topology.Entry>> map = this.topology.asMap();

if (map.isEmpty()) {
throw new Exception("Service not found for '" + name + "'");
}

Optional<Topology.Entry> seOptional = map
.get(name)
.stream()
.findFirst();

Topology.Entry serviceEntry = seOptional.orElseThrow(() -> new Exception("Service not found for '" + name + "'"));

return new URI("http", null, serviceEntry.getAddress(), serviceEntry.getPort(), null, null, null);
}

private String message(String time, String url) {
return "The date and time at " + url + " is " + time;
}

private ManagedExecutorService executorService() throws Exception {
InitialContext ctx = new InitialContext();
return (ManagedExecutorService) ctx.lookup("java:jboss/ee/concurrency/executor/default");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ejm.chapter5.resteasyclient;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
* @author Ken Finnigan
*/
@Path("/")
public interface TimeService {

@GET
@Produces(MediaType.TEXT_PLAIN)
String getTime();
}
46 changes: 46 additions & 0 deletions chapter5/ribbon-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>ejm</groupId>
<artifactId>chapter5</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>chapter5-ribbon-client</artifactId>
<name>Chapter 5: Netflix Ribbon Client</name>
<packaging>war</packaging>

<build>
<finalName>ribbonclient</finalName>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ribbon</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ejm.chapter5.ribbonclient;

import javax.enterprise.concurrent.ManagedExecutorService;
import javax.naming.InitialContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;

import io.netty.buffer.ByteBuf;
import rx.Observable;

/**
* @author Ken Finnigan
*/
@Path("/")
public class MessageResource {

@GET
@Path("/sync")
public String getMessageSync() throws Exception {
ByteBuf buf = TimeService.INSTANCE.getTime().execute();
return message(extractResult(buf));
}

@GET
@Path("/async")
public void getMessageAsync(@Suspended final AsyncResponse asyncResponse) throws Exception {
executorService().submit(() -> {
Observable<ByteBuf> obs = TimeService.INSTANCE.getTime().toObservable();

obs.subscribe(
(result) -> {
asyncResponse.resume(message(extractResult(result)));
},
asyncResponse::resume
);
});
}

private String message(String time) {
return "The date and time is " + time;
}

private String extractResult(ByteBuf result) {
byte[] bytes = new byte[result.readableBytes()];
result.readBytes(bytes);
return new String(bytes);
}

private ManagedExecutorService executorService() throws Exception {
InitialContext ctx = new InitialContext();
return (ManagedExecutorService) ctx.lookup("java:jboss/ee/concurrency/executor/default");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ejm.chapter5.ribbonclient;

import com.netflix.ribbon.Ribbon;
import com.netflix.ribbon.RibbonRequest;
import com.netflix.ribbon.proxy.annotation.Http;
import com.netflix.ribbon.proxy.annotation.ResourceGroup;
import com.netflix.ribbon.proxy.annotation.TemplateName;
import io.netty.buffer.ByteBuf;

/**
* @author Ken Finnigan
*/
@ResourceGroup(name = "time")
public interface TimeService {

TimeService INSTANCE = Ribbon.from(TimeService.class);

@TemplateName("getTime")
@Http(
method = Http.HttpMethod.GET,
uri = "/"
)
RibbonRequest<ByteBuf> getTime();
}
47 changes: 47 additions & 0 deletions chapter5/time/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>ejm</groupId>
<artifactId>chapter5</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>chapter5-time</artifactId>
<name>Chapter 5: Time Microservice</name>
<packaging>war</packaging>

<build>
<finalName>time</finalName>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<properties>
<swarm.port.offset>1</swarm.port.offset>
</properties>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit b4ae6b9

Please sign in to comment.