mqtt客户端和ssh框架整合以及从mqtt服务器获取的数据如何保存到数据库实例

一.mqtt客户端和ssh框架整合

1.在maven的pom.xml文件中加入依赖jar包 


        
			org.springframework.integration
			spring-integration-mqtt
			4.1.0.RELEASE
			
				
					org.eclipse.paho
					mqtt-client
				
			
		
		
		
			org.fusesource.mqtt-client
			mqtt-client
			1.14
		
		
			org.eclipse.paho
			org.eclipse.paho.client.mqttv3
			1.2.0
		

2.写mqtt客户端的配置文件



 
	
	
		
		
	
 
	
	
		
 


 
 
 

 3.在spring框架的配置文件(applicationContext.xml)中引入mqtt.xml配置文件,有可能要加入



					

 二.mqtt服务器获取的数据如何保存到数据库

注意:因为ssh三层与mqtt实现类没有关系,mqtt实现类要用三层关系来保存数据,需要getBean()方法获得Spring管理的Bean的对象(也就是获取Spring的上下文环境ApplicationContext详情链接:

1.https://blog.csdn.net/fubo1990/article/details/79648766

2.https://blog.csdn.net/yang123111/article/details/32099329

3.getBean详解https://blog.csdn.net/qq_23927391/article/details/80625578)(为啥这么做的原因不清楚,请教大佬)

代码:

static String paths[] = {"applicationContext.xml"};
    static ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);  
    static basisMessService basisMessService1 = ctx.getBean(basisMessService.class);

!.在Action类中实现mqtt.xml中的消息处理类

直接按照常规basisMessService不能调用service层,要用Spring管理的Bean的对象basisMessService1才可以调用Service层。

mqtt客户端和ssh框架整合以及从mqtt服务器获取的数据如何保存到数据库实例_第1张图片

2.在service层进行处理逻辑然后调用Dao层保存数据,调用Dao层如上要用Spring管理的Bean的对象调用

private basisMessDao basisMessDao;
     static String paths[] = {"applicationContext.xml"};
     static ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
     static deviceMessDao deviceMessDao1 = ctx.getBean(deviceMessDao.class);
     static basisMessDao basisMessDao1 = ctx.getBean(basisMessDao.class);
     static forstDao forstDao1 = ctx.getBean(forstDao.class);
	@Override
	public void saveData(String[] splited) {
		basisMess basis1=new basisMess();
		deviceMess device1=new deviceMess();
		forst forst=new forst();
		basis1.setLongitude(splited[0]);
	    basis1.setLatitude(splited[1]);
	    basisMess basismess=basisMessDao1.savebasisMess(basis1);
	    basis1.setBid(basismess.getBid());
	    Date date = new Date();
	    Timestamp timeStamep = new Timestamp(date.getTime());
	    WeatherUtil weath = new WeatherUtil();
        String weather = weath.getWeather("XX");
        String len[] = weather.split("#");
        System.out.println("XXXX"+len[6]);
        device1.setWeather(len[6]);
	    device1.setTemperature(splited[2]);
	    device1.setTrend(splited[3]);
	    device1.setTime(timeStamep);
	    device1.setBasisMess(basis1);
	    System.out.println("XXXXXXXXXXXXXXXX");
		deviceMessDao1.savedeviceMess(device1);
		if(splited[2].contains("-")==true)
	    {
	    	forst.setGrade("XXXXXX");
	    }
	    else 
	    	forst.setGrade("XXXXXX");
		forst.setTime(timeStamep);
		forst.setBasisMess(basis1);
	    forstDao1.saveforst(forst);
	}

 

你可能感兴趣的:(mqtt客户端和ssh框架整合以及从mqtt服务器获取的数据如何保存到数据库实例)