Commit 528952fd034ac58410041a6435a9943615c84b0e
1 parent
8f8d69ce
1.车辆提供刷新车辆缓存接口
Showing
1 changed file
with
87 additions
and
46 deletions
src/main/java/com/bsth/server_rs/base_info/car/CarRestService.java
| 1 | -package com.bsth.server_rs.base_info.car; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSON; | |
| 4 | -import com.alibaba.fastjson.TypeReference; | |
| 5 | -import com.bsth.server_rs.base_info.car.buffer.CarBufferData; | |
| 6 | -import com.bsth.server_rs.base_info.dto.BusCardDto; | |
| 7 | -import org.apache.commons.lang3.StringEscapeUtils; | |
| 8 | - | |
| 9 | -import javax.ws.rs.*; | |
| 10 | -import javax.ws.rs.core.MediaType; | |
| 11 | -import java.util.List; | |
| 12 | -import java.util.Map; | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * Created by panzhao on 2017/3/30. | |
| 16 | - */ | |
| 17 | -@Path("/car") | |
| 18 | -@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) | |
| 19 | -public class CarRestService { | |
| 20 | - | |
| 21 | - @GET | |
| 22 | - @Path("/all") | |
| 23 | - public List<Car> findAll(){ | |
| 24 | - return CarBufferData.findAll(); | |
| 25 | - } | |
| 26 | - | |
| 27 | - @GET | |
| 28 | - @Path("/company/{companyId}") | |
| 29 | - public List<Car> findByCompany(@PathParam("companyId") String companyId) { | |
| 30 | - return companyId.equals("-9999") ? CarBufferData.findAll() : CarBufferData.findByCompany(companyId); | |
| 31 | - } | |
| 32 | - | |
| 33 | - @GET | |
| 34 | - @Path("/{nbbm}") | |
| 35 | - public Car findOne(@PathParam("nbbm") String nbbm) { | |
| 36 | - return CarBufferData.findOne(nbbm); | |
| 37 | - } | |
| 38 | - | |
| 39 | - @POST | |
| 40 | - @Path("/setCards") | |
| 41 | - public Map<String, Object> multiPostCards(String bodyStr){ | |
| 42 | - bodyStr = StringEscapeUtils.unescapeHtml4(bodyStr); | |
| 43 | - List<BusCardDto> list = JSON.parseObject(bodyStr, new TypeReference<List<BusCardDto>>() {}); | |
| 44 | - return CarBufferData.multiSaveCards(list); | |
| 45 | - } | |
| 46 | -} | |
| 1 | +package com.bsth.server_rs.base_info.car; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.alibaba.fastjson.TypeReference; | |
| 5 | +import com.bsth.server_rs.base_info.car.buffer.CarBufferData; | |
| 6 | +import com.bsth.server_rs.base_info.car.buffer.CarRefreshThread; | |
| 7 | +import com.bsth.server_rs.base_info.dto.BusCardDto; | |
| 8 | +import org.apache.commons.lang3.StringEscapeUtils; | |
| 9 | +import org.slf4j.Logger; | |
| 10 | +import org.slf4j.LoggerFactory; | |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | + | |
| 13 | +import javax.ws.rs.*; | |
| 14 | +import javax.ws.rs.core.MediaType; | |
| 15 | +import java.util.HashMap; | |
| 16 | +import java.util.List; | |
| 17 | +import java.util.Map; | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * Created by panzhao on 2017/3/30. | |
| 21 | + */ | |
| 22 | +@Path("/car") | |
| 23 | +@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) | |
| 24 | +public class CarRestService { | |
| 25 | + | |
| 26 | + private final static Logger log = LoggerFactory.getLogger(CarRestService.class); | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * 上一次刷新数据时间(refresh) | |
| 30 | + */ | |
| 31 | + private long lastTime; | |
| 32 | + | |
| 33 | + @Autowired | |
| 34 | + private CarRefreshThread carRefreshThread; | |
| 35 | + | |
| 36 | + @GET | |
| 37 | + @Path("/all") | |
| 38 | + public List<Car> findAll(){ | |
| 39 | + return CarBufferData.findAll(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @GET | |
| 43 | + @Path("/company/{companyId}") | |
| 44 | + public List<Car> findByCompany(@PathParam("companyId") String companyId) { | |
| 45 | + return companyId.equals("-9999") ? CarBufferData.findAll() : CarBufferData.findByCompany(companyId); | |
| 46 | + } | |
| 47 | + | |
| 48 | + @GET | |
| 49 | + @Path("/{nbbm}") | |
| 50 | + public Car findOne(@PathParam("nbbm") String nbbm) { | |
| 51 | + return CarBufferData.findOne(nbbm); | |
| 52 | + } | |
| 53 | + | |
| 54 | + @POST | |
| 55 | + @Path("/setCards") | |
| 56 | + public Map<String, Object> multiPostCards(String bodyStr){ | |
| 57 | + bodyStr = StringEscapeUtils.unescapeHtml4(bodyStr); | |
| 58 | + List<BusCardDto> list = JSON.parseObject(bodyStr, new TypeReference<List<BusCardDto>>() {}); | |
| 59 | + return CarBufferData.multiSaveCards(list); | |
| 60 | + } | |
| 61 | + | |
| 62 | + @GET | |
| 63 | + @Path("/refresh") | |
| 64 | + public Map<String, Object> refresh() { | |
| 65 | + Map<String, Object> result = new HashMap<>(); | |
| 66 | + | |
| 67 | + if (System.currentTimeMillis() - lastTime < 30000) { | |
| 68 | + lastTime = System.currentTimeMillis(); | |
| 69 | + result.put("errCode", "C0001"); | |
| 70 | + result.put("msg", "刷新频率过高"); | |
| 71 | + | |
| 72 | + return result; | |
| 73 | + } | |
| 74 | + try { | |
| 75 | + lastTime = System.currentTimeMillis(); | |
| 76 | + carRefreshThread.run(); | |
| 77 | + result.put("errCode", "0"); | |
| 78 | + result.put("msg", "成功"); | |
| 79 | + } catch (Exception e) { | |
| 80 | + log.error("refresh exception", e); | |
| 81 | + result.put("errCode", "S0001"); | |
| 82 | + result.put("msg", e.getMessage()); | |
| 83 | + } | |
| 84 | + | |
| 85 | + return result; | |
| 86 | + } | |
| 87 | +} | ... | ... |