Skip to content
This repository has been archived by the owner on Jul 27, 2018. It is now read-only.

Commit

Permalink
JAV-326 fix pr
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyangyong committed Sep 28, 2017
1 parent af800f7 commit cc941d2
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 211 deletions.
192 changes: 96 additions & 96 deletions seckill-query-service/pom.xml
Original file line number Diff line number Diff line change
@@ -1,113 +1,113 @@
<?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">
<parent>
<artifactId>seckill</artifactId>
<groupId>io.servicecomb.poc.demo</groupId>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
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">
<parent>
<artifactId>seckill</artifactId>
<groupId>io.servicecomb.poc.demo</groupId>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>seckill-query-service</artifactId>
<artifactId>seckill-query-service</artifactId>


<dependencies>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependencies>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>

<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>

<dependency>
<groupId>io.servicecomb.poc.demo</groupId>
<artifactId>seckill-event-store</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>io.servicecomb.poc.demo</groupId>
<artifactId>docker-build-config</artifactId>
<version>0.2.0-SNAPSHOT</version>
</mixin>
</mixins>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>perf</id>
<dependencies>
<dependency>
<groupId>io.servicecomb.poc.demo</groupId>
<artifactId>seckill-event-store</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<groupId>io.kamon</groupId>
<artifactId>kamon-log-reporter_2.11</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>io.kamon</groupId>
<artifactId>kamon-statsd_2.11</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>io.kamon</groupId>
<artifactId>kamon-autoweave_2.11</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>io.servicecomb.poc.demo</groupId>
<artifactId>docker-build-config</artifactId>
<version>0.2.0-SNAPSHOT</version>
</mixin>
</mixins>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>perf</id>
<dependencies>
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-log-reporter_2.11</artifactId>
</dependency>
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-statsd_2.11</artifactId>
</dependency>
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-autoweave_2.11</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
</dependencies>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@

package io.servicecomb.poc.demo;

import io.servicecomb.springboot.starter.provider.EnableServiceComb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import io.servicecomb.springboot.starter.provider.EnableServiceComb;

@SpringBootApplication
public class QueryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(QueryServiceApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(QueryServiceApplication.class, args);
}

@Configuration
@Profile("cse")
@EnableServiceComb
class ServiceCombConfig {
// Here we just enable ServiceComb by default
}
@Configuration
@Profile("cse")
@EnableServiceComb
class ServiceCombConfig {
// Here we just enable ServiceComb by default
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,73 @@

package io.servicecomb.poc.demo.seckill;

import io.servicecomb.poc.demo.seckill.entities.CouponEntity;
import io.servicecomb.poc.demo.seckill.entities.PromotionEntity;
import io.servicecomb.poc.demo.seckill.repositories.spring.SpringCouponRepository;
import io.servicecomb.poc.demo.seckill.repositories.spring.SpringPromotionRepository;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.util.*;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import io.servicecomb.poc.demo.seckill.entities.CouponEntity;
import io.servicecomb.poc.demo.seckill.entities.PromotionEntity;
import io.servicecomb.poc.demo.seckill.repositories.spring.SpringCouponRepository;
import io.servicecomb.poc.demo.seckill.repositories.spring.SpringPromotionRepository;

public class SecKillEventPoller<T> {

private final SpringCouponRepository<T> couponRepository;
private final SpringPromotionRepository promotionRepository;

private final Map<T, Queue<CouponEntity<T>>> customerCoupons = new ConcurrentHashMap<>();
private volatile List<PromotionEntity> activePromotions = new LinkedList<>();
private final int pollingInterval;
private int loadedCouponEntityId = 0;

SecKillEventPoller(
SpringCouponRepository<T> couponRepository,
SpringPromotionRepository promotionRepository,
int pollingInterval) {
this.couponRepository = couponRepository;
this.promotionRepository = promotionRepository;
this.pollingInterval = pollingInterval;
}
private final SpringCouponRepository<T> couponRepository;

void reloadScheduler() {
final Runnable executor = () -> {
populateCoupons();
reloadActivePromotions();
};
Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(executor, 0, pollingInterval, MILLISECONDS);
}
private final SpringPromotionRepository promotionRepository;

public Collection<CouponEntity<T>> getCustomerCoupons(T customerId) {
return customerCoupons.getOrDefault(customerId, null);
}
private final Map<T, Queue<CouponEntity<T>>> customerCoupons = new ConcurrentHashMap<>();

public Collection<PromotionEntity> getActivePromotions() {
return activePromotions;
}
private volatile List<PromotionEntity> activePromotions = new LinkedList<>();

private void populateCoupons() {
List<CouponEntity<T>> couponEntities = couponRepository.findByIdGreaterThan(loadedCouponEntityId);
for (CouponEntity<T> couponEntity : couponEntities) {
customerCoupons.computeIfAbsent(couponEntity.getCustomerId(), id -> new ConcurrentLinkedQueue<>())
.add(couponEntity);
loadedCouponEntityId = couponEntity.getId();
}
}
private final int pollingInterval;

private int loadedCouponEntityId = 0;

private void reloadActivePromotions() {
List<PromotionEntity> promotions = new LinkedList<>();
promotionRepository.findAll().forEach(promotions::add);
activePromotions = promotions;
SecKillEventPoller(
SpringCouponRepository<T> couponRepository,
SpringPromotionRepository promotionRepository,
int pollingInterval) {
this.couponRepository = couponRepository;
this.promotionRepository = promotionRepository;
this.pollingInterval = pollingInterval;
}

void reloadScheduler() {
final Runnable executor = () -> {
populateCoupons();
reloadActivePromotions();
};
Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(executor, 0, pollingInterval, MILLISECONDS);
}

public Collection<CouponEntity<T>> getCustomerCoupons(T customerId) {
return customerCoupons.getOrDefault(customerId, null);
}

public Collection<PromotionEntity> getActivePromotions() {
return activePromotions;
}

private void populateCoupons() {
List<CouponEntity<T>> couponEntities = couponRepository.findByIdGreaterThan(loadedCouponEntityId);
for (CouponEntity<T> couponEntity : couponEntities) {
customerCoupons.computeIfAbsent(couponEntity.getCustomerId(), id -> new ConcurrentLinkedQueue<>())
.add(couponEntity);
loadedCouponEntityId = couponEntity.getId();
}
}

private void reloadActivePromotions() {
List<PromotionEntity> promotions = new LinkedList<>();
promotionRepository.findAll().forEach(promotions::add);
activePromotions = promotions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package io.servicecomb.poc.demo.seckill;

import io.servicecomb.poc.demo.seckill.repositories.spring.SpringCouponRepository;
import io.servicecomb.poc.demo.seckill.repositories.spring.SpringPromotionRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.servicecomb.poc.demo.seckill.repositories.spring.SpringCouponRepository;
import io.servicecomb.poc.demo.seckill.repositories.spring.SpringPromotionRepository;

@Configuration
class SecKillQueryConfig {

Expand Down

0 comments on commit cc941d2

Please sign in to comment.