增加关闭redis池资源
This commit is contained in:
parent
2da88e0656
commit
fdacd1b4ab
|
|
@ -96,8 +96,21 @@ public class RedisStreamUtil {
|
||||||
Map<String,String> map = new HashMap<String,String>();
|
Map<String,String> map = new HashMap<String,String>();
|
||||||
map.put("trans_"+trandId+"@"+userId, transName);
|
map.put("trans_"+trandId+"@"+userId, transName);
|
||||||
Jedis jedis = jedisPool.getResource();
|
Jedis jedis = jedisPool.getResource();
|
||||||
|
|
||||||
|
try{
|
||||||
StreamEntryID id =jedis.xadd(koneStream, new StreamEntryID().NEW_ENTRY, map);
|
StreamEntryID id =jedis.xadd(koneStream, new StreamEntryID().NEW_ENTRY, map);
|
||||||
log.info(userId+"成功增加:trans_"+trandId+"@"+userId+":::"+transName+"[StreamEntryID:"+id+"]");
|
log.info(userId+"成功增加:trans_"+trandId+"@"+userId+":::"+transName+"[StreamEntryID:"+id+"]");
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error(userId+"失败增加:trans"+trandId+"@"+userId+":::"+transName+"]");
|
||||||
|
}finally {
|
||||||
|
if (jedis != null) {
|
||||||
|
try {
|
||||||
|
jedis.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -136,9 +149,21 @@ public class RedisStreamUtil {
|
||||||
Map<String,String> map = new HashMap<String,String>();
|
Map<String,String> map = new HashMap<String,String>();
|
||||||
map.put("job_"+jobId+"@"+userId, jobName);
|
map.put("job_"+jobId+"@"+userId, jobName);
|
||||||
Jedis jedis = jedisPool.getResource();
|
Jedis jedis = jedisPool.getResource();
|
||||||
|
try{
|
||||||
StreamEntryID id = jedis.xadd(koneStream, new StreamEntryID().NEW_ENTRY, map);
|
StreamEntryID id = jedis.xadd(koneStream, new StreamEntryID().NEW_ENTRY, map);
|
||||||
log.info(userId+"成功增加:job_"+jobId+"@"+userId+":::"+jobName+"[StreamEntryID:"+id+"]");
|
log.info(userId+"成功增加:job_"+jobId+"@"+userId+":::"+jobName+"[StreamEntryID:"+id+"]");
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error(userId+"失败增加:job_"+jobId+"@"+userId+":::"+jobName+"]");
|
||||||
|
}finally {
|
||||||
|
if (jedis != null) {
|
||||||
|
try {
|
||||||
|
jedis.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @Description: 循环重队列中读消息
|
* @Description: 循环重队列中读消息
|
||||||
|
|
@ -164,18 +189,23 @@ public class RedisStreamUtil {
|
||||||
log.error("addKettleJob()获取主机ip异常:"+e);
|
log.error("addKettleJob()获取主机ip异常:"+e);
|
||||||
}
|
}
|
||||||
while (true){
|
while (true){
|
||||||
|
|
||||||
Jedis jedis = jedisPool.getResource();
|
Jedis jedis = jedisPool.getResource();
|
||||||
|
if(jedis ==null){
|
||||||
|
return;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
Map<String,StreamEntryID> t = new HashMap<String,StreamEntryID>();
|
Map<String,StreamEntryID> t = new HashMap<String,StreamEntryID>();
|
||||||
List<Map.Entry<String, List<StreamEntry>>> list = new ArrayList<Map.Entry<String, List<StreamEntry>>>();
|
List<java.util.Map.Entry<java.lang.String,java.util.List<redis.clients.jedis.StreamEntry>>> list = new ArrayList<java.util.Map.Entry<java.lang.String,java.util.List<redis.clients.jedis.StreamEntry>>>();
|
||||||
t.put(koneStream, null);//null 则为 > 重头读起,也可以为$接受新消息,还可以是上一次未读完的消息id
|
t.put(koneStream, null);//null 则为 > 重头读起,也可以为$接受新消息,还可以是上一次未读完的消息id
|
||||||
Map.Entry e = null;
|
Map.Entry<java.lang.String,redis.clients.jedis.StreamEntryID> e = null;
|
||||||
for(Map.Entry c:t.entrySet()){
|
for(Map.Entry<java.lang.String,redis.clients.jedis.StreamEntryID> c:t.entrySet()){
|
||||||
e=c;
|
e=c;
|
||||||
}
|
}
|
||||||
//noAck为false的话需要手动ack,true则自动ack. commsumer新建的方式为xreadgroup。
|
//noAck为false的话需要手动ack,true则自动ack. commsumer新建的方式为xreadgroup。
|
||||||
log.info("开始读消息");
|
log.info("开始读消息");
|
||||||
try{
|
try{
|
||||||
list = jedis.xreadGroup(koneGroup, koneConsumer, 1, 30000, false, e);
|
list = jedis.xreadGroup(koneGroup, koneConsumer, 1, 30000L, false, e);
|
||||||
|
|
||||||
}catch (Exception ex){
|
}catch (Exception ex){
|
||||||
log.error("超时了!!!!!!!!");
|
log.error("超时了!!!!!!!!");
|
||||||
|
|
@ -186,7 +216,7 @@ public class RedisStreamUtil {
|
||||||
}else{
|
}else{
|
||||||
for (Map.Entry m : list) {
|
for (Map.Entry m : list) {
|
||||||
if (m.getValue() instanceof ArrayList) {
|
if (m.getValue() instanceof ArrayList) {
|
||||||
List<StreamEntry> l = (List) m.getValue();
|
List<StreamEntry> l = (List<StreamEntry>) m.getValue();
|
||||||
Map<String, String> result = l.get(0).getFields();
|
Map<String, String> result = l.get(0).getFields();
|
||||||
for (Map.Entry entry : result.entrySet()) {
|
for (Map.Entry entry : result.entrySet()) {
|
||||||
System.out.println(entry.getKey() + "---" + entry.getValue());
|
System.out.println(entry.getKey() + "---" + entry.getValue());
|
||||||
|
|
@ -211,7 +241,17 @@ public class RedisStreamUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}finally {
|
||||||
|
if (jedis != null) {
|
||||||
|
try {
|
||||||
|
jedis.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue