Java Infinite Loop

Asked

Viewed 272 times

4

Guys how do I take the Infinite Loop out of this class? Every time it is the last database information he keeps sending email.

public class Dia06 {  

public static final long TEMPO = (14 * 1440);
protected static final String Class = null;

public static void main(String[] args) throws SQLException {  
    Connection conexao = new Conexao().getConnection();
    PreparedStatement stmt = conexao.prepareStatement(
            "SELECT P.duplic, "
                    + "       P.dtemissao, "
                    + "       P.dtvenc, "
                    + "       P.valor, "
                    + "       C.cliente, "
                    + "       P.obs2, "
                    + "       c.email, "
                    + "       P.codcli, "
                    + "       C.telent, "
                    + "       Nvl(P.valordesc, 0) VLDESC, "
                    + "       P.codusur, "
                    + "       P.prest, "
                    + "       P.codcob, "
                    + "       C.ieent, "
                    + "       C.bloqueio, "
                    + "       P.duplic "
                    + "       || '-' "
                    + "       || P.prest          AS TITULO, "
                    + "       P.numtransvenda, "
                    + "       P.codfilial "
                    + "FROM   pcprest P, "
                    + "       pcclient C, "
                    + "       pccob B, "
                    + "       pcfilial F "
                    + "WHERE  P.codcob = B.codcob "
                    + "       AND P.codcli = C.codcli "
                    + "       AND P.codfilial = F.codigo "
                    + "       AND  F.CODIGO = 3 "
                    + "       AND p.codcob NOT IN ( 'DEVP', 'DEVT', 'BNF', 'BNFT', "
                    + "                             'BNFR', 'BNTR', 'BNRP', 'CRED', 'DESD' ) "
                    + "       AND P.dtpag IS NULL "
                    + "       AND p.dtvenc = To_date(SYSDATE)-6");

    ResultSet rs = stmt.executeQuery();

    while (rs.next()){

        final String cliente = rs.getString("cliente");
        final String dtvenc = rs.getString("dtvenc");
        final String valor   = rs.getString("valor");
        //final String EMAIL   = rs.getString("EMAIL");
        //final String duplic   = rs.getString("duplic");

        Timer timer = null;  
        if (timer == null) {  
            timer = new Timer();  
            TimerTask tarefa = new TimerTask() {  
                @SuppressWarnings("deprecation")

                public void run() {   


                    //criar um update depois que enviar o email.
                    try {  

                        MultiPartEmail emai = new MultiPartEmail(); //classe utilizada para permitir anexos no email  
                        emai.setDebug(true);  
                        emai.setHostName("192.168.2.200"); //servidor SMTP. Aqui usamos um do Gmail  
                        emai.setSmtpPort(25);
                        emai.setAuthentication("[email protected]", "898999"); // login e senha da conta Gmail  
                        emai.setSSL(false); //Autentica��o de seguran�a SSL setada como True  
                        //emai.addTo(EMAIL);
                        emai.addTo("[email protected]"); //nome do email que vai receber o bkp do banco de dados. Pode ser o seu para teste  
                        emai.setFrom("[email protected]"); //endere�o de email do remetente  
                        emai.setSubject("Cobran�a de D�vida Ativa"); //assunto  
                        emai.setMsg("Prezado( a ) Senhor( a ): " + cliente

                                +"Prezados,\n\n"

                                +"  Conforme descrito no comunicado emitido anteriormente, informamos que, em virtude do n�o pagamento do t�tulo do t�tulo tal,\n "
                                +"  com vencimento para "+dtvenc+", no valor de R$"+valor+", o(s) mesmo(s) ser�(�o) levado(s) a protesto, e os dados de v. senhoria\n "
                                +"  inscritos nos cadastros de restri��o ao cr�dito.\n\n"

                                +" 

                                +"Certos de sua compreens�o, mantemo-nos � disposi��o para a resolu��o desta pend�ncia. \n\n"

                                +"Por tratar-se de aviso autom�tico enviado eletronicamente, para o caso do referido pagamento j� ter sido efetuado, pe�o que entrem \n"
                                +"em contato para normaliza��o da situa��o descrita. \n\n"

                                +"Bras�lia-DF, xx de Xxxxxxxx de 2014. \n\n"

                                +"Atenciosamente, \n\n"); //carta03

                        emai.send(); //envia o email  
                    } catch (EmailException e) { //exception caso aconte�a algum erro ao enviar o email  
                        e.printStackTrace() ;
                        //chamar metodo  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  

                    System.out.println("saida 1");
                }  
            };  

            timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO);  
        }  
    }  

}

`

  • from what I can tell you’re using thread...try to stop using task.();

  • Thanks Peter for the answer more where should I put? I tried before I could not, sorry the ignorance more I’m beginner in the area.

  • tries to put after emai.send();

  • ta printing "output 1"?

  • Pedro didn’t work out, he’s making a mistake.

  • yes you are leaving saida 1

  • blz then.. when finished do not call the scheduleAtFixedRate method, as it will repeat the execution .

  • more with the scheduleAtFixedRate method it does the time count to send the letter!!! or to wrong?

  • after System.out.println("output 1"); use (timer.Cancel(); and timer.purge();) or System.Exit(0); see this link... http://www.dsc.ufcg.edu.br/~Jacques/courses/map/html/threads/timer.html

  • see my answer to see if it helps you.

  • to help and be helped when necessary^^

Show 6 more comments

2 answers

2


You can try in this part of the code add the following lines:

                    } catch (Exception e) {  
                    e.printStackTrace();  
                }  

                System.out.println("saida 1");
                timer.cancel();
                timer.purge();
            }  
        };  
    timer.schedule(tarefa, TEMPO).

1

The Timer.scheduleAtFixedRate(Timertask, long, long) method is used to repeat the task endlessly for a certain period of time, which in this case is every 20 seconds (14 * 1440 = 20160 milliseconds ~ 20 seconds). I think you don’t need Timer in this case. Just send an email after another.

If you’re using Timer to facilitate thread creation, consider using the method Timer.Chedule(Timertask, long).

Finally, a warning to:

Timer timer = null;
if (timer == null) {
    timer = new Timer();
...
}

This construction is unnecessary, because timer will always be null in if. Change for only:

Timer timer = new Timer();

In that case, teams will always be non-zero.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.