<?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>
<!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
<!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
@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)); } }
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; } }
@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.
mvn spring-boot:run
mvn clean package
Java -jar target/gs-rest-service-0.1.0.jar
@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'.
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'.
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'.
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'.
public interface UserRepository extends CrudRepository<User, Long> { Long countByLastname(String lastname); }Query creation from method names
public interface UserRepository extends CrudRepository<User, Long> { Long deleteByLastname(String lastname); List<User> removeByLastname(String lastname); }You can see them pressing 's'.
Hazelcast is an in-memory open source software data grid based on Java. -- Wikipedia
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"); } }
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()); } }
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(); } } }
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; } }
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")); }
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); } }
Also known as the time to embarrass yourself.
You can see them pressing 's'.