Home > Back-end >  Database configuration mongo address dynamically generated error instance objects
Database configuration mongo address dynamically generated error instance objects

Time:01-04



When I was in a XXL - time job tasks dynamically generated using aop method instance objects, but will quote connection timeout, don't know what went wrong there

This is my code to the
 
@ Around (routeMongoDB "()")
Public Object routeMongoDB (ProceedingJoinPoint joinPoint) {
The Object result=null;
Object [] args=joinPoint. GetArgs ();
String db=String. The valueOf (args [0]).
The Object o=joinPoint. GetTarget ();
Field [] fields=o.g etClass () getDeclaredFields ();
MultiMongoTemplate mongoTemplate=null;
Try {
For (Field Field: fields) {
Field. SetAccessible (true);
Object fieldObject=field. The get (o);
The Class fieldclass=fieldObject. GetClass ();
//find the Template variable
If (fieldclass==MongoTemplate. Class | | fieldclass==MultiMongoTemplate. Class) {
//find the corresponding MongFactory
SimpleMongoClientDbFactory SimpleMongoClientDbFactory=(SimpleMongoClientDbFactory) templateMuliteMap. Get (db);
//instantiate
If (simpleMongoClientDbFactory==null) {
//based on dbname query data corresponding address
This. Uri=testMongoUriDao. FindByAppId (db). GetUri ();
//replace data
SimpleMongoClientDbFactory=new simpleMongoClientDbFactory (this) uri) replace (" # ", db));
TemplateMuliteMap. Put (db, simpleMongoClientDbFactory);
}
//if the first time, the assignment into custom MongoTemplate subclass
If (fieldclass==MongoTemplate. Class) {
MongoTemplate=new MultiMongoTemplate (simpleMongoClientDbFactory);
} else if (fieldclass==MultiMongoTemplate. Class) {
MongoTemplate=(MultiMongoTemplate fieldObject);
}
//set MongoFactory
MongoTemplate. SetMongoDbFactory (simpleMongoClientDbFactory);
//to assignment
Field. The set (o, mongoTemplate);
break;
}
}
Try {
Result=joinPoint. Proceed ();
//clean the ThreadLocal variable
MongoTemplate. RemoveMongoDbFactory ();
{} the catch (Throwable t)
Logger. The error (" ", t);
}
} the catch (Exception e) {
Logger. The error (" ", e);
}

return result;
}
  • Related