Skip to content

Instantly share code, notes, and snippets.

@juliano
Created September 16, 2012 21:03
Show Gist options
  • Save juliano/3734386 to your computer and use it in GitHub Desktop.
Save juliano/3734386 to your computer and use it in GitHub Desktop.
Concurrent Services
apply plugin: 'eclipse'
apply plugin: 'jetty'
apply plugin: 'maven'
sourceCompatibility = 1.5
targetCompatibility = 1.5
version = '0.1-SNAPSHOT'
group = 'br.com.simpledev'
repositories {
mavenCentral()
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
configurations {
provided
}
sourceSets {
main { compileClasspath += configurations.provided }
test { runtimeClasspath += configurations.provided }
}
eclipse.classpath.plusConfigurations += configurations.provided
dependencies {
provided 'javax.servlet:servlet-api:2.5'
}
[jettyRunWar, jettyRun]*.contextPath = '/'
package br.com.concurrent5.service;
import java.util.Random;
public class RandomService {
private final String name;
public RandomService(final String name) {
this.name = name;
}
public String execute() {
try {
Thread.sleep(new Random().nextInt(5000));
return name + " ok";
} catch (InterruptedException e) {
return name + " falhou";
}
}
}
package br.com.concurrent5.service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
public class ServiceCaller implements Callable<String> {
private final RandomService service;
public ServiceCaller(final RandomService service) {
this.service = service;
}
public String call() throws Exception {
return service.execute();
}
public static List<ServiceCaller> asList(final RandomService... services) {
List<ServiceCaller> callers = new ArrayList<ServiceCaller>();
for (RandomService rs : services) {
callers.add(new ServiceCaller(rs));
}
return callers;
}
}
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>br.com.concurrent5.controller.WebApplication</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>
package br.com.concurrent5.controller;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.concurrent5.service.RandomService;
import br.com.concurrent5.service.ServiceCaller;
public class WebApplication extends HttpServlet {
private volatile ThreadPoolExecutor executor;
public void init() throws ServletException {
super.init();
executor = new ThreadPoolExecutor(6, 6, 0, SECONDS,
new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE));
}
protected void doGet(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
List<ServiceCaller> tasks = ServiceCaller.asList(new RandomService(
"Servico 1"), new RandomService("Servico 2"),
new RandomService("Servico 3"), new RandomService("Servico 4"),
new RandomService("Servico 5"));
List<String> results = new ArrayList<String>();
try {
results = getResults(tasks);
} catch (InterruptedException e) {
throw new ServletException(e);
}
for (String r : results) {
response.getWriter().write(r + " - ");
}
}
private List<String> getResults(final List<ServiceCaller> tasks)
throws InterruptedException {
List<String> results = new ArrayList<String>();
List<Future<String>> futures = executor.invokeAll(tasks, 2, SECONDS);
for (Future<String> f : futures) {
if (f.isDone()) {
try {
results.add(f.get());
} catch (CancellationException e) {
results.add("Ficou pra trás");
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
return results;
}
public void destroy() {
super.destroy();
executor.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment