Skip to content

Commit

Permalink
push code
Browse files Browse the repository at this point in the history
push the complete code
  • Loading branch information
lewky committed Oct 10, 2017
1 parent c6acdfa commit d717f78
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 16 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
<scope>provided</scope>
</dependency>

<!--5:spring依赖 -->
Expand Down Expand Up @@ -129,7 +129,7 @@
<version>4.1.7.RELEASE</version>
</dependency>

<!--添加redis依赖 -->
<!--添加Redis依赖 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
Expand All @@ -147,6 +147,12 @@
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>
<!--导入apache工具类 -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
<build>
<finalName>seckill</finalName>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/lewis/dao/SeckillDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;

Expand Down Expand Up @@ -34,4 +35,10 @@ public interface SeckillDao {
* @return
*/
List<Seckill> queryAll(@Param("offset") int offset, @Param("limit") int limit);

/**
* 使用储存过程执行秒杀
* @param paramMap
*/
void killByProcedure(Map<String,Object> paramMap);
}
10 changes: 10 additions & 0 deletions src/main/java/com/lewis/mapper/SeckillDao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@
limit #{offset},#{limit}
</select>

<!--调用储存过程 -->
<select id="killByProcedure" statementType="CALLABLE">
CALL execute_seckill(
#{seckillId,jdbcType=BIGINT,mode=IN},
#{phone,jdbcType=BIGINT,mode=IN},
#{killTime,jdbcType=TIMESTAMP,mode=IN},
#{result,jdbcType=INTEGER,mode=OUT}
)
</select>

</mapper>
10 changes: 10 additions & 0 deletions src/main/java/com/lewis/service/SeckillService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public interface SeckillService {
*/
SeckillExecution executeSeckill(long seckillId, long userPhone, String md5) throws SeckillException,
RepeatKillException, SeckillCloseException;

/**
* 调用存储过程来执行秒杀操作,不需要抛出异常
*
* @param seckillId 秒杀的商品ID
* @param userPhone 手机号码
* @param md5 md5加密值
* @return 根据不同的结果返回不同的实体信息
*/
SeckillExecution executeSeckillProcedure(long seckillId,long userPhone,String md5);
}
38 changes: 31 additions & 7 deletions src/main/java/com/lewis/service/impl/SeckillServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lewis.service.impl;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,7 +23,9 @@

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Component @Service @Repository @Controller
@Service
Expand All @@ -42,8 +45,8 @@ public class SeckillServiceImpl implements SeckillService {
// @Resource
private SuccessKilledDao successKilledDao;

// @Autowired
// private RedisDao redisDao;
@Autowired
private RedisDao redisDao;

public List<Seckill> getSeckillList() {
return seckillDao.queryAll(0, 4);
Expand All @@ -57,18 +60,16 @@ public Exposer exportSeckillUrl(long seckillId) {
// 优化点:缓存优化:超时的基础上维护一致性
// 1.访问redi

// Seckill seckill = redisDao.getSeckill(seckillId);
Seckill seckill=seckillDao.queryById(seckillId);
Seckill seckill = redisDao.getSeckill(seckillId);
if (seckill == null) {
// 2.访问数据库
seckill = seckillDao.queryById(seckillId);
if (seckill == null) {// 说明查不到这个秒杀产品的记录
return new Exposer(false, seckillId);
}
/*else {
} else {
// 3.放入redis
redisDao.putSeckill(seckill);
}*/
}
}

// 若是秒杀未开启
Expand Down Expand Up @@ -139,4 +140,27 @@ public SeckillExecution executeSeckill(long seckillId, long userPhone, String md
}

}

@Override
public SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5) {
if (md5 == null || !md5.equals(getMD5(seckillId))) {
return new SeckillExecution(seckillId, SeckillStatEnum.DATE_REWRITE);
}
Date killTime = new Date();
Map<String, Object> map = new HashMap<>();
map.put("seckillId", seckillId);
map.put("phone", userPhone);
map.put("killTime", killTime);
map.put("result", null);
// 执行储存过程,result被复制
seckillDao.killByProcedure(map);
// 获取result
int result = MapUtils.getInteger(map, "result", -2);
if (result == 1) {
SuccessKilled successKilled = successKilledDao.queryByIdWithSeckill(seckillId, userPhone);
return new SeckillExecution(seckillId, SeckillStatEnum.SUCCESS, successKilled);
} else {
return new SeckillExecution(seckillId, SeckillStatEnum.stateOf(result));
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/lewis/web/SeckillController.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long s
}

try {
SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5);
//这里改为调用存储过程
// SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5);
SeckillExecution execution = seckillService.executeSeckillProcedure(seckillId, userPhone, md5);
return new SeckillResult<SeckillExecution>(true, execution);
}catch (RepeatKillException e1)
{
Expand Down
13 changes: 7 additions & 6 deletions src/main/resources/spring/spring-dao.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!--配置整合mybatis过程
1.配置数据库相关参数-->
Expand Down Expand Up @@ -54,9 +55,9 @@
<property name="basePackage" value="com.lewis.dao"/>
</bean>

<!--redisDao-->
<!-- <bean id="redisDao" class="com.lewis.dao.cache.RedisDao">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="6379"/>
</bean> -->
<!--redisDao -->
<bean id="redisDao" class="com.lewis.dao.cache.RedisDao">
<constructor-arg index="0" value="localhost" />
<constructor-arg index="1" value="6379" />
</bean>
</beans>
45 changes: 45 additions & 0 deletions src/test/java/com/lewis/dao/cache/RedisDaoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.lewis.dao.cache;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lewis.dao.SeckillDao;
import com.lewis.entity.Seckill;

@RunWith(SpringJUnit4ClassRunner.class)
// 告诉junit spring的配置文件
@ContextConfiguration({ "classpath:spring/spring-dao.xml" })
public class RedisDaoTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private long id = 1001;

@Autowired
private RedisDao redisDao;

@Autowired
private SeckillDao seckillDao;

@Test
public void testSeckill() {

Seckill seckill = redisDao.getSeckill(id);
if (seckill == null) {
seckill = seckillDao.queryById(id);
if (seckill != null) {
String result = redisDao.putSeckill(seckill);
logger.info("result={}", result);
seckill = redisDao.getSeckill(id);
logger.info("seckill={}", seckill);
}
}
}

}
13 changes: 13 additions & 0 deletions src/test/java/com/lewis/service/SeckillServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,17 @@ public void testSeckillLogic() throws Exception {
logger.warn("exposer={}", exposer);
}
}

@Test
public void executeSeckillProcedure(){
long seckillId = 1001;
long phone = 13680115101L;
Exposer exposer = seckillService.exportSeckillUrl(seckillId);
if (exposer.isExposed()) {
String md5 = exposer.getMd5();
SeckillExecution execution = seckillService.executeSeckillProcedure(seckillId, phone, md5);
logger.info("execution={}", execution);
}
}

}

0 comments on commit d717f78

Please sign in to comment.