boot_talk



boot_talk

0 0


boot_talk

Talk about spring boot

On Github jimternet / boot_talk

Boot

Cassandra

Hazelcast

I

❤ ❤ ❤ ❤ ❤ ❤

Spring Boot

BLAH BALH BLAH You can see them pressing 's'.

Boot-Pom1

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.1.RELEASE</version>
    </parent>

Boot-Pom2

<!-- Add typical dependencies for a web application -->
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

Boot-Pom3

<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Boot-GreetingController

@RestController
public class GreetingController {

  private static final String template = "Hello, %s!";
  private final AtomicLong counter = new AtomicLong();

  @RequestMapping("/greeting")
  public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
    String.format(template, name));
  }
}

Boot-GreetingPojo

public class Greeting {

  private final long id;
  private final String content;

  public Greeting(long id, String content) {
    this.id = id;
    this.content = content;
  }
  public long getId() {
    return id;
  }
  public String getContent() {
    return content;
  }
}

Boot-GreetingApplication

@RestController
@ComponentScan
@EnableAutoConfiguration
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
The main() method defers to the SpringApplication helper class, providing Application.class as an argument to its run() method. This tells Spring to read the annotation metadata from Application and to manage it as a component in the Spring application context. The @ComponentScan annotation tells Spring to search recursively through the hello package and its children for classes marked directly or indirectly with Spring’s @Component annotation. This directive ensures that Spring finds and registers the GreetingController, because it is marked with @RestController, which in turn is a kind of @Component annotation. The @EnableAutoConfiguration annotation switches on reasonable default behaviors based on the content of your classpath. For example, because the application depends on the embeddable version of Tomcat (tomcat-embed-core.jar), a Tomcat server is set up and configured with reasonable defaults on your behalf. And because the application also depends on Spring MVC (spring-webmvc.jar), a Spring MVC DispatcherServlet is configured and registered for you — no web.xml necessary! Auto-configuration is a powerful, flexible mechanism. See the API documentation for further details.

Boot-Build-and-Run

Maven plugin

mvn spring-boot:run

Maven Build

mvn clean package

Maven Build

Java -jar target/gs-rest-service-0.1.0.jar

I

mostly

Cassandra

Cassandra-What-is-it

  • a hot topic at Target maybe too hot
  • Decentralized
  • Scalability
  • Fault-tolerant
  • MapReduce support Dont use Hadoop though
  • Query language
Put your speaker notes here. You can see them pressing 's'.

Cassandra-Spring-Data

  • To the rescue at least I like it :)
  • Spring conventions - repositories, templates
  • Annotated POJOs
  • Cluster and Session support
  • QueryBuilders Put your speaker notes here. You can see them pressing 's'.

Cassandra-PersonPojo

@Table
public class Person {

  @PrimaryKey
  private String id;

  private String name;
  private int age;

  //constructor(s), getters, setters

}
Put your speaker notes here. You can see them pressing 's'.

Cassandra-CrudRepository

public interface CrudRepository<T, ID extends Serializable>
extends Repository<T, ID> {

  <S extends T> S save(S entity);

  T findOne(ID primaryKey);

  Iterable<T> findAll();

  Long count();

  void delete(T entity);

  boolean exists(ID primaryKey);  

  //
}
Put your speaker notes here. You can see them pressing 's'.

Cassandra-PagingAndSorting

public interface PagingAndSortingRepository<T, ID extends Serializable>
extends CrudRepository<T, ID> {

  Iterable<T> findAll(Sort sort);

  Page<T> findAll(Pageable pageable);
}
Put your speaker notes here. You can see them pressing 's'.

Cassandra-Main

  Cluster cluster = Cluster.builder().addContactPoints(InetAddress.getLocalHost()).build();
  Session session = cluster.connect("mykeyspace");
  CassandraOperations cassandraOps = new CassandraTemplate(session);
  cassandraOps.insert(new Person("1234567890", "David", 40));
  Select s = QueryBuilder.select().from("person");
  s.where(QueryBuilder.eq("id", "1234567890"));
  LOG.info(cassandraOps.queryForObject(s, Person.class).getId());
  cassandraOps.truncate("person");
Put your speaker notes here. You can see them pressing 's'.

Cassandra-Repositories-continued

public interface UserRepository extends CrudRepository<User, Long> {

  Long countByLastname(String lastname);
}
Query creation from method names

Cassandra-Repositories-continued

public interface UserRepository extends CrudRepository<User, Long> {

  Long deleteByLastname(String lastname);

  List<User> removeByLastname(String lastname);

}
You can see them pressing 's'.

Hazelcast

I

Hazelcast

This is a new Markdown slide

What is Hazelcast?

Hazelcast is an in-memory open source software data grid based on Java. -- Wikipedia

Hazelcast-Features

  • In-Memory Java Caching
  • NoSQL Key Value Store
  • 4MB JAR library
  • Drop-In with no dependencies
  • Apache 2.0 License

What is Hazelcast?

Scalable

Highly available

Highly performant

Hazelcast-Java-Functions

  • The following slides are just taken from hazelcast.org
  • I'm often lazy

Hazelcast- Map

public class DistributedMap {
  public static void main(String[] args) {
    Config config = new Config();
    HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
    ConcurrentMap<String, String> map = h.getMap("my-distributed-map");
    map.put("key", "value");
    map.get("key");

    //Concurrent Map methods
    map.putIfAbsent("somekey", "somevalue");
    map.replace("key", "value", "newvalue");
  }
}

Hazelcast-Topic

public class DistributedTopic implements MessageListener<String> {
  public static void main(String[] args) {
    Config config = new Config();
    HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
    ITopic<String> topic = h.getTopic("my-distributed-topic");
    topic.addMessageListener(new DistributedTopic());
    topic.publish("Hello to distributed world");
  }

@Override
  public void onMessage(Message<String> message) {
    System.out.println("Got message " + message.getMessageObject());
  }
}

Hazelcast-Lock

public class DistributedLock {
  public static void main(String[] args) {
    Config config = new Config();
    HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
    Lock lock = h.getLock("my-distributed-lock");
    lock.lock();
    try {
      //do something here
      } finally {
        lock.unlock();
      }
    }
  }

Hazelcast-EntryProcessor

public static class IncEntryProcessor extends AbstractEntryProcessor<String, Integer> {
    @Override
    public Object process(Map.Entry<String, Integer> entry) {
      int oldValue = entry.getValue();
      int newValue = oldValue + 1;
      entry.setValue(newValue);
      return null;
    }
  }

Hazelcast-EntryProcessor2

public static void main(String[] args) {
  HazelcastInstance hz = Hazelcast.newHazelcastInstance();
  IMap<String, Integer> map = hz.getMap("map");
  map.put("key", 0);
  map.executeOnKey("key", new IncEntryProcessor());
  System.out.println("new value:" + map.get("key"));
}

Hazelcast-Aggregation

public class DistributedAggregation {
  public static void main(String[] args) {
    Config config = new Config();
    HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
    IMap<String, Integer> salaries = h.getMap("salaries");
    fillInSalaries(salaries);
    Supplier<String, Integer, Integer> supplier = Supplier.all();
    int sum = salaries.aggregate(supplier, Aggregations.integerSum());
    System.out.println("Aggregated sum: " + sum);
  }
}

Hazelcast-OtherTypes

  • Multimap
  • JCache
  • Queue
  • Executor Service
  • IAtomicLong
  • Aggregation
  • Map Reduce

Me

  • Email: james.beyers@target.com /jim.beyers@target.com
  • GitHub: Follow jimternet for repository updates
  • Twitter: @jimternet
Put your speaker notes here. You can see them pressing 's'.

Demo

Also known as the time to embarrass yourself.

You can see them pressing 's'.