NodeReceiverJMS.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */



  20. package org.openspcoop2.pdd.core.node;

  21. import org.slf4j.Logger;
  22. import org.openspcoop2.core.id.IDSoggetto;
  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.pdd.core.AbstractCore;
  25. import org.openspcoop2.pdd.core.JMSReceiver;
  26. import org.openspcoop2.pdd.core.PdDContext;
  27. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  28. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  29. import org.openspcoop2.pdd.services.core.RicezioneBuste;
  30. import org.openspcoop2.pdd.services.core.RicezioneBusteMessage;
  31. import org.openspcoop2.pdd.services.core.RicezioneContenutiApplicativi;
  32. import org.openspcoop2.pdd.services.core.RicezioneContenutiApplicativiMessage;
  33. import org.openspcoop2.protocol.engine.constants.Costanti;
  34. import org.openspcoop2.protocol.sdk.Busta;

  35. /**
  36.  * Classe utilizzata per la ricezione di messaggi contenuti nell'architettura di OpenSPCoop (versione JMS).
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */

  42. public class NodeReceiverJMS extends AbstractCore implements INodeReceiver{

  43.     private static OpenSPCoop2Properties openspcoopProperties =
  44.         OpenSPCoop2Properties.getInstance();
  45.    
  46.     /**
  47.      * Ricezione di un messaggio  
  48.      *
  49.      * @param codicePorta Codice Porta per cui effettuare la receive
  50.      * @param idModulo Nodo destinatario per cui effettuare la ricezione.
  51.      * @param timeout Timeout sulla ricezione
  52.      * @param checkInterval Intervallo di check sulla coda
  53.      * @return true se la ricezione JMS e' andata a buon fine, false altrimenti.
  54.      *
  55.      */
  56.     @Override
  57.     public Object receive(MsgDiagnostico msgDiag, IDSoggetto codicePorta,String idModulo,String idMessaggio,
  58.             long timeout,long checkInterval) throws NodeException,NodeTimeoutException{
  59.         /* ------------  Lettura parametri del messaggio ricevuto e ValidityCheck -------------- */
  60.         Object objReturn = null;
  61.         Logger log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  62.         try{
  63.             JMSReceiver receiverJMS = new JMSReceiver(codicePorta,idModulo,NodeReceiverJMS.openspcoopProperties.singleConnectionNodeReceiver(),log, PdDContext.getValue(org.openspcoop2.core.constants.Costanti.ID_TRANSAZIONE, this.getPddContext()));
  64.             while(true){ // un msg deve essere ricevuto per forza.
  65.                
  66.                 String strMessageSelector = "ID = '"+idMessaggio+"'";
  67.                 msgDiag.highDebug("Ricezione messaggio selector["+strMessageSelector+"]....");
  68.                 if(receiverJMS.receive(idModulo,strMessageSelector,
  69.                         timeout,checkInterval) == false){
  70.                     throw new javax.jms.JMSException(receiverJMS.getErrore());
  71.                 }
  72.                 msgDiag.highDebug("Ricezione effettuata");
  73.                 objReturn = receiverJMS.getObjectReceived();    
  74.                 if(objReturn == null)
  75.                     throw new javax.jms.JMSException("Oggetto ricevuto dalla coda jms is null");
  76.                
  77.                 String idRisposta = null;
  78.                 String tipoMessaggio = null;
  79.                 if(idModulo.startsWith(RicezioneContenutiApplicativi.ID_MODULO)){
  80.                     msgDiag.highDebug("Lettura risposta per RicezioneContenutiApplicativi...");
  81.                     idRisposta = ((RicezioneContenutiApplicativiMessage)objReturn).getIdBustaRisposta();
  82.                     tipoMessaggio = Costanti.INBOX;
  83.                     msgDiag.highDebug("Lettura risposta per RicezioneContenutiApplicativi effettuata");
  84.                 }
  85.                 else if(idModulo.startsWith(RicezioneBuste.ID_MODULO)){
  86.                     msgDiag.highDebug("Lettura risposta per RicezioneBuste...");
  87.                     Busta bustaRisposta =  ((RicezioneBusteMessage)objReturn).getBustaRisposta();
  88.                     if(bustaRisposta!=null)
  89.                         idRisposta = bustaRisposta.getID();
  90.                     else
  91.                         idRisposta = ((RicezioneBusteMessage)objReturn).getIdMessaggioSblocco();
  92.                     tipoMessaggio = Costanti.OUTBOX;
  93.                     msgDiag.highDebug("Lettura risposta per RicezioneBuste effettuata");
  94.                 }
  95.                 if(TransactionManager.validityCheck(msgDiag,idModulo,idRisposta,
  96.                         tipoMessaggio,receiverJMS.getIdHeaderJMS(),this.getPddContext())==false){
  97.                     msgDiag.highDebug("Messaggio con id["+idMessaggio+"] non ha superato il validity check");
  98.                     log.error("Messaggio con id["+idMessaggio+"] non ha superato il validity check");
  99.                 }else{
  100.                     break;
  101.                 }
  102.             }
  103.         } catch (Exception e) {
  104.             if(e.getMessage()!=null){
  105.                 if(e.getMessage().indexOf("Messaggio non ricevuto")>=0){
  106.                     throw new NodeTimeoutException("Risposta per la gestione della richiesta, "
  107.                             +e.getMessage(),e);
  108.                 }else{
  109.                     throw new NodeException("Riscontrato errore nella ricezione del messaggio di risposta per la gestione della richiesta:"
  110.                             +e.getMessage(),e);
  111.                 }
  112.             }else{
  113.                 throw new NodeException("Riscontrato errore nella ricezione del messaggio di risposta per la gestione della richiesta",e);
  114.             }
  115.            
  116.         }
  117.         return objReturn;
  118.     }
  119.    
  120. }