forked from digitalsonic/geektime-spring-family
-
Notifications
You must be signed in to change notification settings - Fork 0
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
0e1a4d8
commit 332c4ce
Showing
78 changed files
with
2,059 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
/build/ |
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,54 @@ | ||
<?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> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>geektime.spring.data.reactive</groupId> | ||
<artifactId>mongodb-demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>mongodb-demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.joda</groupId> | ||
<artifactId>joda-money</artifactId> | ||
<version>1.0.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
101 changes: 101 additions & 0 deletions
101
...-demo/src/main/java/geektime/spring/data/reactive/mongodbdemo/MongodbDemoApplication.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,101 @@ | ||
package geektime.spring.data.reactive.mongodbdemo; | ||
|
||
import geektime.spring.data.reactive.mongodbdemo.converter.MoneyReadConverter; | ||
import geektime.spring.data.reactive.mongodbdemo.converter.MoneyWriteConverter; | ||
import geektime.spring.data.reactive.mongodbdemo.model.Coffee; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.joda.money.CurrencyUnit; | ||
import org.joda.money.Money; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; | ||
import org.springframework.data.mongodb.core.convert.MongoCustomConversions; | ||
import org.springframework.data.mongodb.core.query.Update; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import static org.springframework.data.mongodb.core.query.Criteria.where; | ||
import static org.springframework.data.mongodb.core.query.Query.query; | ||
|
||
@SpringBootApplication | ||
@Slf4j | ||
public class MongodbDemoApplication implements ApplicationRunner { | ||
@Autowired | ||
private ReactiveMongoTemplate mongoTemplate; | ||
private CountDownLatch cdl = new CountDownLatch(2); | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MongodbDemoApplication.class, args); | ||
} | ||
|
||
@Bean | ||
public MongoCustomConversions mongoCustomConversions() { | ||
return new MongoCustomConversions( | ||
Arrays.asList(new MoneyReadConverter(), | ||
new MoneyWriteConverter())); | ||
} | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
// startFromInsertion(() -> log.info("Runnable")); | ||
startFromInsertion(() -> { | ||
log.info("Runnable"); | ||
decreaseHighPrice(); | ||
}); | ||
|
||
log.info("after starting"); | ||
|
||
// decreaseHighPrice(); | ||
|
||
cdl.await(); | ||
} | ||
|
||
private void startFromInsertion(Runnable runnable) { | ||
mongoTemplate.insertAll(initCoffee()) | ||
.publishOn(Schedulers.elastic()) | ||
.doOnNext(c -> log.info("Next: {}", c)) | ||
.doOnComplete(runnable) | ||
.doFinally(s -> { | ||
cdl.countDown(); | ||
log.info("Finnally 1, {}", s); | ||
}) | ||
.count() | ||
.subscribe(c -> log.info("Insert {} records", c)); | ||
} | ||
|
||
private void decreaseHighPrice() { | ||
mongoTemplate.updateMulti(query(where("price").gte(3000L)), | ||
new Update().inc("price", -500L) | ||
.currentDate("updateTime"), Coffee.class) | ||
.doFinally(s -> { | ||
cdl.countDown(); | ||
log.info("Finnally 2, {}", s); | ||
}) | ||
.subscribe(r -> log.info("Result is {}", r)); | ||
} | ||
|
||
private List<Coffee> initCoffee() { | ||
Coffee espresso = Coffee.builder() | ||
.name("espresso") | ||
.price(Money.of(CurrencyUnit.of("CNY"), 20.0)) | ||
.createTime(new Date()) | ||
.updateTime(new Date()) | ||
.build(); | ||
Coffee latte = Coffee.builder() | ||
.name("latte") | ||
.price(Money.of(CurrencyUnit.of("CNY"), 30.0)) | ||
.createTime(new Date()) | ||
.updateTime(new Date()) | ||
.build(); | ||
|
||
return Arrays.asList(espresso, latte); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...src/main/java/geektime/spring/data/reactive/mongodbdemo/converter/MoneyReadConverter.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,12 @@ | ||
package geektime.spring.data.reactive.mongodbdemo.converter; | ||
|
||
import org.joda.money.CurrencyUnit; | ||
import org.joda.money.Money; | ||
import org.springframework.core.convert.converter.Converter; | ||
|
||
public class MoneyReadConverter implements Converter<Long, Money> { | ||
@Override | ||
public Money convert(Long aLong) { | ||
return Money.ofMinor(CurrencyUnit.of("CNY"), aLong); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...rc/main/java/geektime/spring/data/reactive/mongodbdemo/converter/MoneyWriteConverter.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,11 @@ | ||
package geektime.spring.data.reactive.mongodbdemo.converter; | ||
|
||
import org.joda.money.Money; | ||
import org.springframework.core.convert.converter.Converter; | ||
|
||
public class MoneyWriteConverter implements Converter<Money, Long> { | ||
@Override | ||
public Long convert(Money money) { | ||
return money.getAmountMinorLong(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
... 5/mongodb-demo/src/main/java/geektime/spring/data/reactive/mongodbdemo/model/Coffee.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,21 @@ | ||
package geektime.spring.data.reactive.mongodbdemo.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.joda.money.Money; | ||
|
||
import java.util.Date; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class Coffee { | ||
private String id; | ||
private String name; | ||
private Money price; | ||
private Date createTime; | ||
private Date updateTime; | ||
} |
1 change: 1 addition & 0 deletions
1
Chapter 5/mongodb-demo/src/main/resources/application.properties
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 @@ | ||
spring.data.mongodb.uri=mongodb://springbucks:springbucks@localhost:27017/springbucks |
16 changes: 16 additions & 0 deletions
16
.../src/test/java/geektime/spring/data/reactive/mongodbdemo/MongodbDemoApplicationTests.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,16 @@ | ||
package geektime.spring.data.reactive.mongodbdemo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class MongodbDemoApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |
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,25 @@ | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
/build/ |
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,70 @@ | ||
<?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> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>geektime.spring</groupId> | ||
<artifactId>springbucks</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>springbucks</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.joda</groupId> | ||
<artifactId>joda-money</artifactId> | ||
<version>1.0.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jadira.usertype</groupId> | ||
<artifactId>usertype.core</artifactId> | ||
<version>6.0.1.GA</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>p6spy</groupId> | ||
<artifactId>p6spy</artifactId> | ||
<version>3.8.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
50 changes: 50 additions & 0 deletions
50
...ormance-aspect-demo/src/main/java/geektime/spring/springbucks/SpringBucksApplication.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,50 @@ | ||
package geektime.spring.springbucks; | ||
|
||
import geektime.spring.springbucks.model.Coffee; | ||
import geektime.spring.springbucks.model.CoffeeOrder; | ||
import geektime.spring.springbucks.model.OrderState; | ||
import geektime.spring.springbucks.repository.CoffeeRepository; | ||
import geektime.spring.springbucks.service.CoffeeOrderService; | ||
import geektime.spring.springbucks.service.CoffeeService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.EnableAspectJAutoProxy; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@EnableTransactionManagement | ||
@SpringBootApplication | ||
@EnableJpaRepositories | ||
@EnableAspectJAutoProxy | ||
public class SpringBucksApplication implements ApplicationRunner { | ||
@Autowired | ||
private CoffeeRepository coffeeRepository; | ||
@Autowired | ||
private CoffeeService coffeeService; | ||
@Autowired | ||
private CoffeeOrderService orderService; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBucksApplication.class, args); | ||
} | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
log.info("All Coffee: {}", coffeeRepository.findAll()); | ||
|
||
Optional<Coffee> latte = coffeeService.findOneCoffee("Latte"); | ||
if (latte.isPresent()) { | ||
CoffeeOrder order = orderService.createOrder("Li Lei", latte.get()); | ||
log.info("Update INIT to PAID: {}", orderService.updateState(order, OrderState.PAID)); | ||
log.info("Update PAID to INIT: {}", orderService.updateState(order, OrderState.INIT)); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.