Spring使用event-stream進行數(shù)據推送
前端使用EventSource方式向后臺發(fā)送請求,后端接收到之后使用event-stream方式流式返回??梢詰迷跁r鐘、逐字聊天等場景。
前端js示例代碼(向后臺請求數(shù)據,并展示到“id=date”的div上)
<script type="text/javascript">
if (typeof (EventSource) !== "undefined") {
var eventSource = new EventSource("${root}/test/getDate");
eventSource.onmessage = function (event) {
document.getElementById("date").innerHTML = event.data;
}
eventSource.addEventListener('error', function (event) {
console.log("錯誤:" + event);
});
eventSource.addEventListener('open', function (event) {
console.log("建立連接:" + event);
});
} else {
document.getElementById("date").innerHTML = "抱歉,您的瀏覽器不支持EventSource事件 ...";
}
</script>后端java示例
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
@Controller
@RequestMapping(value = "/test")
public class TestController {
@ResponseBody
@RequestMapping(value = "/getDate", produces = "text/event-stream;charset=UTF-8")
public void getDate(HttpServletResponse response) throws Exception {
System.out.println("getDate event start");
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
response.setStatus(200);
for (int i = 0; i < 10; i++) {
response.getWriter().write("data:" + new Date() + "\n\n");
response.getWriter().flush();
Thread.sleep(1000);
}
response.getWriter().close();
System.out.println("getDate event end");
}
}除了是這個方法,我們也可以使用Spring的定時器定時推送數(shù)據
定時器編寫
1.在配置文件里添加相關配置
?
<!-- 定時器開關 -->
<task:annotation-driven />
<!-- 自動掃描的包名 -->
<bean id="myTaskXml" class="定時方法所在的類的完整類名"></bean>
<task:scheduled-tasks>
<task:scheduled ref="myTaskXml" method="appInfoAdd" cron="*/5 * * * * ?"/>
</task:scheduled-tasks>
?上面的代碼設置的是每5秒執(zhí)行一次appInfoAdd方法,cron字段表示的是時間的設置,分為:秒,分,時,日,月,周,年。
2.定時方法的編寫
Logger log = Logger.getLogger(TimerTask.class);;
public void apiInfoAdd(){
InputStream in = TimerTask.class.getClassLoader().getResourceAsStream("/system.properties");
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
String key = prop.getProperty("DLKEY");
log.info("key--" + key);
//鎖數(shù)據操作
List<GbEsealInfo> list = this.esealService.loadBySendFlag("0");
if (list == null) {
log.info("沒有需要推送的鎖數(shù)據");
}else{
JsonObject json = new JsonObject();
JsonArray ja = new JsonArray();
json.add("esealList", ja);
for(int i = 0; i < list.size(); i++) {
GbEntInfo ent = this.entService.loadByPk(list.get(i).getEntId());
JsonObject esealJson = getEsealJson(list.get(i), ent);
ja.add(esealJson);
}
String data = json.toString();
log.info("鎖數(shù)據--" + data);
HttpDeal hd = new HttpDeal();
Map<String,String> map = new HashMap<String, String>();
map.put("key",key);
map.put("data", data);
hd.post(prop.getProperty("ESEALURL"), map);
log.info(hd.responseMsg);
JsonObject returnData = new JsonParser().parse(hd.responseMsg).getAsJsonObject();
if (("\"10000\"").equals(returnData.get("code").toString())){
log.info("鎖數(shù)據推送成功");
for(int i = 0; i < list.size(); i++){
list.get(i).setSendFlag("1");
int res = this.esealService.updateSelectiveByPk(list.get(i));
if(res == 0){
log.info("第" + (i+1) + "條鎖數(shù)據的sendflag字段的狀態(tài)值修改失敗");
continue;
}
}
}
}這個過程包括讀取配置文件里的key,從數(shù)據庫讀取數(shù)據(根據數(shù)據的字段判斷是否需要推送),使用GSON把數(shù)據拼裝成上一篇文章里的data的形式data={"esealList":[{},{},{}]},使用HttpClient執(zhí)行帶參數(shù)的POST方法,訪問POST接口,根據返回信息判斷狀態(tài)。
3.HttpClient里的帶參POST方法
public class HttpDeal {
public String responseMsg;
public String post(String url,Map<String, String> params){
//實例化httpClient
CloseableHttpClient httpclient = HttpClients.createDefault();
//實例化post方法
HttpPost httpPost = new HttpPost(url);
//處理參數(shù)
List<NameValuePair> nvps = new ArrayList <NameValuePair>();
Set<String> keySet = params.keySet();
for(String key : keySet) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
//結果
CloseableHttpResponse response = null;
String content="";
try {
//提交的參數(shù)
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
//將參數(shù)給post方法
httpPost.setEntity(uefEntity);
//執(zhí)行post方法
response = httpclient.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
content = EntityUtils.toString(response.getEntity(),"utf-8");
responseMsg = content;
//System.out.println(content);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static void main(String[] args) {
HttpDeal hd = new HttpDeal();
Map<String,String> map = new HashMap();
map.put("key","5c07db6e22c780e76824c88b2e65e9d9");
map.put("data", "{'esealList':[{'esealId':'1','entName':'q','entName':企業(yè),'id':'qqww','id':'123', 'customsCode':'qq','fax':'021-39297127'}]}");
hd.post("http://localhost:8080/EportGnssWebDL/api/CopInfoAPI/esealInfoAdd.json",map);
}
}4.輔助類
/**
* 輔助類:鎖接口需要的字段
* @param eseal
* @param ent
* @return
*/
private JsonObject getEsealJson(GbEsealInfo eseal, GbEntInfo ent){
JsonObject j = new JsonObject();
j.addProperty("esealId", eseal.getEsealId());
j.addProperty("vehicleNo", eseal.getVehicleNo());
j.addProperty("customsCode", eseal.getCustomsCode());
j.addProperty("simNo", eseal.getSimNo());
j.addProperty("entName", ent.getEntName());
j.addProperty("customsName", eseal.getCustomsName());
j.addProperty("leadingOfficial", ent.getLeadingOfficial());
j.addProperty("contact", ent.getContact());
j.addProperty("address", ent.getAddress());
j.addProperty("mail", ent.getMail());
j.addProperty("phone", ent.getPhone());
j.addProperty("fax", ent.getFax());
return j;
}以上就是Spring使用event-stream進行數(shù)據推送的詳細內容,更多關于Spring event-stream數(shù)據推送的資料請關注腳本之家其它相關文章!
相關文章
帶你了解Java數(shù)據結構和算法之前綴,中綴和后綴表達式
這篇文章主要為大家介紹了Java的前綴,中綴和后綴表達式 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
關于Spring的AnnotationAwareAspectJAutoProxyCreator類解析
這篇文章主要介紹了關于Spring的AnnotationAwareAspectJAutoProxyCreator類解析,Spring是一個開源免費的框架 , 容器,是一個輕量級的框架 ,需要的朋友可以參考下2023-05-05
Java實現(xiàn)DES加密與解密,md5加密以及Java實現(xiàn)MD5加密解密類
這篇文章主要介紹了Java實現(xiàn)DES加密與解密,md5加密以及Java實現(xiàn)MD5加密解密類 ,需要的朋友可以參考下2015-11-11
Spring Security實現(xiàn)5次密碼錯誤觸發(fā)賬號自動鎖定功能
在現(xiàn)代互聯(lián)網應用中,賬號安全是重中之重,然而,暴力 破解攻擊依然是最常見的安全威脅之一,攻擊者通過自動化腳本嘗試大量的用戶名和密碼組合,試圖找到漏洞進入系統(tǒng),所以為了解決這一問題,賬號鎖定機制被廣泛應用,本文介紹了Spring Security實現(xiàn)5次密碼錯誤觸發(fā)賬號鎖定功能2024-12-12
mybatis mybatis-plus-generator+clickhouse自動生成代碼案例詳解
這篇文章主要介紹了mybatis mybatis-plus-generator+clickhouse自動生成代碼案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

