-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
851f37d
commit b4ae6b9
Showing
11 changed files
with
408 additions
and
1 deletion.
There are no files selected for viewing
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
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,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> |
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,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> |
96 changes: 96 additions & 0 deletions
96
chapter5/resteasy-client/src/main/java/ejm/chapter5/resteasyclient/MessageResource.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,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"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
chapter5/resteasy-client/src/main/java/ejm/chapter5/resteasyclient/TimeService.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,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(); | ||
} |
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,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> |
55 changes: 55 additions & 0 deletions
55
chapter5/ribbon-client/src/main/java/ejm/chapter5/ribbonclient/MessageResource.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,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"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
chapter5/ribbon-client/src/main/java/ejm/chapter5/ribbonclient/TimeService.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,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(); | ||
} |
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,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> |
Oops, something went wrong.