mqtt初始化

pull/19/head
James-hp 5 years ago
parent e680069929
commit 670740979c

@ -71,7 +71,6 @@
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<!-- SpringBoot 监控客户端 --> <!-- SpringBoot 监控客户端 -->
<dependency> <dependency>
<groupId>de.codecentric</groupId> <groupId>de.codecentric</groupId>
@ -195,6 +194,12 @@
<artifactId>ruoyi-common-redis</artifactId> <artifactId>ruoyi-common-redis</artifactId>
<version>${ruoyi.version}</version> <version>${ruoyi.version}</version>
</dependency> </dependency>
<!-- mqtt服务 -->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-mqtt</artifactId>
<version>${ruoyi.version}</version>
</dependency>
<!-- 系统接口 --> <!-- 系统接口 -->
<dependency> <dependency>

@ -15,6 +15,7 @@
<module>ruoyi-common-swagger</module> <module>ruoyi-common-swagger</module>
<module>ruoyi-common-security</module> <module>ruoyi-common-security</module>
<module>ruoyi-common-datascope</module> <module>ruoyi-common-datascope</module>
<module>ruoyi-common-mqtt</module>
</modules> </modules>
<artifactId>ruoyi-common</artifactId> <artifactId>ruoyi-common</artifactId>

@ -4,7 +4,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -730,9 +729,9 @@ public class ExcelUtil<T>
if (StringUtils.isNotEmpty(name)) if (StringUtils.isNotEmpty(name))
{ {
Class<?> clazz = o.getClass(); Class<?> clazz = o.getClass();
String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); Field field = clazz.getDeclaredField(name);
Method method = clazz.getMethod(methodName); field.setAccessible(true);
o = method.invoke(o); o = field.get(o);
} }
return o; return o;
} }

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ruoyi-common-mqtt</artifactId>
<description>
ruoyi-common-mqtt服务
</description>
<dependencies>
<!-- SpringBoot Boot mqtt -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
</dependency>
<!-- RuoYi Common Core-->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-core</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,7 @@
package com.ruoyi.common.redis.service;
import org.springframework.stereotype.Component;
@Component
public class MqttService {
}

@ -1,71 +1,71 @@
package com.ruoyi.common.redis.configure; package com.ruoyi.common.redis.configure;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException; import org.springframework.data.redis.serializer.SerializationException;
import com.alibaba.fastjson.parser.ParserConfig; import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.nio.charset.Charset; import java.nio.charset.Charset;
/** /**
* Redis使FastJson * Redis使FastJson
* *
* @author ruoyi * @author ruoyi
*/ */
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{ {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private ObjectMapper objectMapper = new ObjectMapper(); private ObjectMapper objectMapper = new ObjectMapper();
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz; private Class<T> clazz;
static static
{ {
ParserConfig.getGlobalInstance().setAutoTypeSupport(true); ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
} }
public FastJson2JsonRedisSerializer(Class<T> clazz) public FastJson2JsonRedisSerializer(Class<T> clazz)
{ {
super(); super();
this.clazz = clazz; this.clazz = clazz;
} }
@Override @Override
public byte[] serialize(T t) throws SerializationException public byte[] serialize(T t) throws SerializationException
{ {
if (t == null) if (t == null)
{ {
return new byte[0]; return new byte[0];
} }
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
} }
@Override @Override
public T deserialize(byte[] bytes) throws SerializationException public T deserialize(byte[] bytes) throws SerializationException
{ {
if (bytes == null || bytes.length <= 0) if (bytes == null || bytes.length <= 0)
{ {
return null; return null;
} }
String str = new String(bytes, DEFAULT_CHARSET); String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz); return JSON.parseObject(str, clazz);
} }
public void setObjectMapper(ObjectMapper objectMapper) public void setObjectMapper(ObjectMapper objectMapper)
{ {
Assert.notNull(objectMapper, "'objectMapper' must not be null"); Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
} }
protected JavaType getJavaType(Class<?> clazz) protected JavaType getJavaType(Class<?> clazz)
{ {
return TypeFactory.defaultInstance().constructType(clazz); return TypeFactory.defaultInstance().constructType(clazz);
} }
} }

@ -1,43 +1,43 @@
package com.ruoyi.common.redis.configure; package com.ruoyi.common.redis.configure;
import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* redis * redis
* *
* @author ruoyi * @author ruoyi
*/ */
@Configuration @Configuration
@EnableCaching @EnableCaching
public class RedisConfig extends CachingConfigurerSupport public class RedisConfig extends CachingConfigurerSupport
{ {
@Bean @Bean
@SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" }) @SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{ {
RedisTemplate<Object, Object> template = new RedisTemplate<>(); RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(connectionFactory);
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper); serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer); template.setValueSerializer(serializer);
// 使用StringRedisSerializer来序列化和反序列化redis的key值 // 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer()); template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet(); template.afterPropertiesSet();
return template; return template;
} }
} }

@ -1,227 +1,227 @@
package com.ruoyi.common.redis.service; package com.ruoyi.common.redis.service;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* spring redis * spring redis
* *
* @author ruoyi * @author ruoyi
**/ **/
@SuppressWarnings(value = { "unchecked", "rawtypes" }) @SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component @Component
public class RedisService public class RedisService
{ {
@Autowired @Autowired
public RedisTemplate redisTemplate; public RedisTemplate redisTemplate;
/** /**
* IntegerString * IntegerString
* *
* @param key * @param key
* @param value * @param value
*/ */
public <T> void setCacheObject(final String key, final T value) public <T> void setCacheObject(final String key, final T value)
{ {
redisTemplate.opsForValue().set(key, value); redisTemplate.opsForValue().set(key, value);
} }
/** /**
* IntegerString * IntegerString
* *
* @param key * @param key
* @param value * @param value
* @param timeout * @param timeout
* @param timeUnit * @param timeUnit
*/ */
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{ {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit); redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
} }
/** /**
* *
* *
* @param key Redis * @param key Redis
* @param timeout * @param timeout
* @return true=false= * @return true=false=
*/ */
public boolean expire(final String key, final long timeout) public boolean expire(final String key, final long timeout)
{ {
return expire(key, timeout, TimeUnit.SECONDS); return expire(key, timeout, TimeUnit.SECONDS);
} }
/** /**
* *
* *
* @param key Redis * @param key Redis
* @param timeout * @param timeout
* @param unit * @param unit
* @return true=false= * @return true=false=
*/ */
public boolean expire(final String key, final long timeout, final TimeUnit unit) public boolean expire(final String key, final long timeout, final TimeUnit unit)
{ {
return redisTemplate.expire(key, timeout, unit); return redisTemplate.expire(key, timeout, unit);
} }
/** /**
* *
* *
* @param key * @param key
* @return * @return
*/ */
public <T> T getCacheObject(final String key) public <T> T getCacheObject(final String key)
{ {
ValueOperations<String, T> operation = redisTemplate.opsForValue(); ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key); return operation.get(key);
} }
/** /**
* *
* *
* @param key * @param key
*/ */
public boolean deleteObject(final String key) public boolean deleteObject(final String key)
{ {
return redisTemplate.delete(key); return redisTemplate.delete(key);
} }
/** /**
* *
* *
* @param collection * @param collection
* @return * @return
*/ */
public long deleteObject(final Collection collection) public long deleteObject(final Collection collection)
{ {
return redisTemplate.delete(collection); return redisTemplate.delete(collection);
} }
/** /**
* List * List
* *
* @param key * @param key
* @param dataList List * @param dataList List
* @return * @return
*/ */
public <T> long setCacheList(final String key, final List<T> dataList) public <T> long setCacheList(final String key, final List<T> dataList)
{ {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList); Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count; return count == null ? 0 : count;
} }
/** /**
* list * list
* *
* @param key * @param key
* @return * @return
*/ */
public <T> List<T> getCacheList(final String key) public <T> List<T> getCacheList(final String key)
{ {
return redisTemplate.opsForList().range(key, 0, -1); return redisTemplate.opsForList().range(key, 0, -1);
} }
/** /**
* Set * Set
* *
* @param key * @param key
* @param dataSet * @param dataSet
* @return * @return
*/ */
public <T> long setCacheSet(final String key, final Set<T> dataSet) public <T> long setCacheSet(final String key, final Set<T> dataSet)
{ {
Long count = redisTemplate.opsForSet().add(key, dataSet); Long count = redisTemplate.opsForSet().add(key, dataSet);
return count == null ? 0 : count; return count == null ? 0 : count;
} }
/** /**
* set * set
* *
* @param key * @param key
* @return * @return
*/ */
public <T> Set<T> getCacheSet(final String key) public <T> Set<T> getCacheSet(final String key)
{ {
return redisTemplate.opsForSet().members(key); return redisTemplate.opsForSet().members(key);
} }
/** /**
* Map * Map
* *
* @param key * @param key
* @param dataMap * @param dataMap
*/ */
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
{ {
if (dataMap != null) { if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap); redisTemplate.opsForHash().putAll(key, dataMap);
} }
} }
/** /**
* Map * Map
* *
* @param key * @param key
* @return * @return
*/ */
public <T> Map<String, T> getCacheMap(final String key) public <T> Map<String, T> getCacheMap(final String key)
{ {
return redisTemplate.opsForHash().entries(key); return redisTemplate.opsForHash().entries(key);
} }
/** /**
* Hash * Hash
* *
* @param key Redis * @param key Redis
* @param hKey Hash * @param hKey Hash
* @param value * @param value
*/ */
public <T> void setCacheMapValue(final String key, final String hKey, final T value) public <T> void setCacheMapValue(final String key, final String hKey, final T value)
{ {
redisTemplate.opsForHash().put(key, hKey, value); redisTemplate.opsForHash().put(key, hKey, value);
} }
/** /**
* Hash * Hash
* *
* @param key Redis * @param key Redis
* @param hKey Hash * @param hKey Hash
* @return Hash * @return Hash
*/ */
public <T> T getCacheMapValue(final String key, final String hKey) public <T> T getCacheMapValue(final String key, final String hKey)
{ {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash(); HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey); return opsForHash.get(key, hKey);
} }
/** /**
* Hash * Hash
* *
* @param key Redis * @param key Redis
* @param hKeys Hash * @param hKeys Hash
* @return Hash * @return Hash
*/ */
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
{ {
return redisTemplate.opsForHash().multiGet(key, hKeys); return redisTemplate.opsForHash().multiGet(key, hKeys);
} }
/** /**
* *
* *
* @param pattern * @param pattern
* @return * @return
*/ */
public Collection<String> keys(final String pattern) public Collection<String> keys(final String pattern)
{ {
return redisTemplate.keys(pattern); return redisTemplate.keys(pattern);
} }
} }

Loading…
Cancel
Save