最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring使用event-stream進行數(shù)據推送

 更新時間:2024年03月25日 09:31:06   作者:zyydd_  
這篇文章主要介紹了Spring使用event-stream進行數(shù)據推送,前端使用EventSource方式向后臺發(fā)送請求,后端接收到之后使用event-stream方式流式返回,文中有相關的代碼示例供大家參考,需要的朋友可以參考下

前端使用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ù)據推送的資料請關注腳本之家其它相關文章!

相關文章

最新評論

孝昌县| 玉树县| 霍邱县| 读书| 云林县| 伊金霍洛旗| 庆云县| 武宁县| 雅安市| 白玉县| 应城市| 宁波市| 临西县| 东丰县| 泰州市| 临江市| 同仁县| 宝鸡市| 五常市| 聂拉木县| 鄂伦春自治旗| 柘荣县| 旬邑县| 南靖县| 石泉县| 芮城县| 深圳市| 哈尔滨市| 门源| 宜春市| 营山县| 荆州市| 桑植县| 南城县| 昆山市| 望谟县| 淳安县| 吉首市| 花莲市| 上饶县| 邮箱|