Developers
Autoscaling Java
Matt Stephenson
Software Engineer, App Engine Java
Autoscaling
Autoscaling
Autoscaling
Autoscaling
Autoscaling in the JVM
Autoscaling in App Engine
Demo
Autoscaling
Cloud Antipatterns
Large Deployments
<dependency>
<groupId>org.grails</groupId>
<artifactId>grails-spring</artifactId>
<version>2.2.0.RC4</version>
</dependency>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>my-framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.2.2.RELEASE</version></dependency>
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.7.Final</version></dependency>
Large Deployments
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.10-b02</version>
</dependency>
pom.xml
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.2.1</version></dependency>
<dependency><!-- Protip : Use the afterburner module --> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-afterburner</artifactId> <version>2.2.1</version></dependency>
Automatic Class Instantiation
package org.example;
@Service
public class SimpleMovieLister {
private final MovieFinder movieFinder;
@Autowired
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
Java
Automatic Class Instantiation
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.example"/>
</beans>
XML
Automatic Class Instantiation
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="SimpleMovieListener" class="org.example.SimpleMovieListener" />
</beans>
XML
Demo
Automatic class instantiation in a Large application
Code Generation
@Aspect
public class ExceptionLoggingInterceptor {
@Around("execution(public * *(..))")
public Object logExceptions(ProceedingJoinPoint pjp) throws Throwable {
try {
return retVal = pjp.proceed();
} catch (Throwable t) {
logger.debug(t);
throw t;
}
}
}
Java
Demo
Code Generation
Managing Shared State
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) ... {
Map<String, String> parameters = request.getParameterMap();
if(parameters.containsKey("Foo")) {
Key barKey = KeyFactory.createKey("Bar", parameters.get("Foo"));
try {
Entity entity = datastoreService.get(barKey);
servletRequest.setAttribute("Baz", entity.getProperty("Baz"));
} catch (EntityNotFoundException e) { ... }
}
filterChain.doFilter(servletRequest, servletResponse);
}
Java
Persisting Configuration
private Optional<String> secret = Optional.absent();
@Override
public void init() throws ServletException {
while (!secret.isPresent()) {
try {
Entity entity = service.get(SHARED_SECRET_KEY);
secret = Optional.<String>fromNullable((String) entity.getProperty("TheSecret"))
} catch (EntityNotFoundException e) {
secret = generateSecret();
}
}
}
Java
Persisting Configuration
private Optional<String> generateSecret() {
Transaction transaction = service.beginTransaction();
try {
String secretKey = generateKey();
Entity entity = new Entity(SHARED_SECRET_KEY);
entity.setProperty("TheSecret", secretKey);
service.put(entity);
transaction.commit();
return Optional.of(secretKey);
} catch (ConcurrentModificationException e) {
return Optional.absent();
} finally { ... }
}
Java
Demo
Shared State and Configuration Data Contention
Tools
Go forth and scale
Matt Stephenson & Ludovic Champenois
Office Hours : Thursday 10:15-11:00am
@ The Cloud Sandbox
Developers