1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| public class JMSMonitor implements Runnable, ReConfigurable {
private static final String IDL = JMSMonitor.class.toString();
private ApplicationContext ctx = null;
private static JmsTemplate jmst = null;
private Configuration config = null;
private String springConfigurationPath = null;
private static boolean active = true;
/**
* Constructeur privé pour initialiser les objets utilisés dans le traitement
*/
public JMSMonitor() {
}
public void run() {
while(true) {
try {
if (active) {
LogJMSItem item = LogJMSQueue.getInstance().take();
JMSMonitor.logJPFM(item.getMsgIn(), item.getExceptionTraitement());
JMSMonitor.logJPFM(item.getMsgOut(), item.getExceptionTraitement());
} else {
if (LogJMSQueue.getInstance().size() > 0)
LogJMSQueue.getInstance().clear();
Thread.sleep(5000);
}
} catch (InterruptedException e) {
Log.error(IDL, "Impossible d'avoir accès à la Queue de données ou impossible d'endormir le Thread (si Logging JMS inactif)");
}
}
}
/**
* Permet de logguer un message provenant de jPFM
* @param msg ISOMsg a logguer
* @param e Exception éventuelle lors du traitement
* @throws Exception
*/
private static void logJPFM(ISOMsg msg, Exception e) {
HashMap<Integer, Object> hm = null;
if (msg != null) {
try {
hm = JMSMonitor.pack(msg);
} catch (Exception e1) {
Log.error(IDL,"Impossible de transformer le message");
JMSMonitor.sendObjectMessage(null,e1);
}
JMSMonitor.sendObjectMessage(hm,e);
} else {
JMSMonitor.sendObjectMessage(null,e);
}
}
/**
* Créé et envoie un message issu de jPFM pour stockage dans la base de données
* @param hm HashMap représentant l'ISOMsg originel
* @param e Exception éventuelle durant le traitement
*/
private static void sendObjectMessage(final HashMap<Integer, Object> hm, final Exception e) {
MessageCreator msgcrea = null;
String hmr = null;
if (hm != null) {
String typeMessage = (String)hm.get(22);
String etatBorne = (String)hm.get(895);
if(typeMessage != null && etatBorne != null && (typeMessage.equals("940") || typeMessage.equals("980")) && !etatBorne.equals("0")) {
//TODO: envoi alerte mail RA
String borne = (String)hm.get(26);
if (borne == null) {borne = (String)hm.get(923);}
if (borne == null) borne = "borne non précisée";
try {
EmailService.sendMail(borne);
} catch (Exception e1) {
Log.error(IDL,"Impossible d'envoyer l'email au RA");
}
Log.info(IDL,"Message de borne HS/KO! Borne concernée: "+borne);
}
msgcrea = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
ObjectMessage omsg = session.createObjectMessage(hm);
if(e!=null) omsg.setStringProperty("EXCEPTION", e.toString());
return omsg;
}
};
} else {
if(e!= null) {
msgcrea = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
ObjectMessage omsg = session.createObjectMessage();
if(e!=null) omsg.setStringProperty("EXCEPTION", e.toString());
return omsg;
}
};
} else {
Log.warn(IDL,"Rien a logguer, le message et l'exception sont null");
}
}
try {
jmst.send(msgcrea);
} catch (JmsException e1) {
Log.error(IDL,"Le message n'a pas pu être envoyé: "+e1.toString()+".\nMessage abandonné.");
}
}
/**
* Méthode de conversion ISOMsg-->HashMap
* @param m ISOMsg à convertir
* @return HashMap représentant l'ISOMsg
* @throws Exception
*/
@SuppressWarnings("unchecked")
private static HashMap<Integer, Object> pack(ISOComponent m) throws Exception{
HashMap<Integer, Object> hm = new HashMap<Integer, Object>();
int cle = 0;
ISOComponent isocomponent;
try {
ISOMsg isoMes = (ISOMsg) m;
Hashtable<Integer, ISOComponent> isoFields = isoMes.getChildren();
for (Enumeration<Integer> e = isoFields.keys(); e.hasMoreElements();) {
cle = e.nextElement().intValue();
isocomponent = (ISOComponent) isoMes.getComponent(cle);
if (isocomponent instanceof ISOField) { // this is a single field
hm.put(cle, (String) isocomponent.getValue());
} else { // this is a submessage
hm.put(cle, pack(isocomponent));
}
}
return hm;
} catch (ISOException e) {
throw new Exception(e);
}
} |
Partager