首页 > 解决方案 > TimerTask Scheduler 在静态引用其他类对象后不起作用

问题描述

我有一个参考其他类的 TimeTask 类。现在,当我创建对另一个类的静态引用时;它只执行该类的静态块并且定时器任务类的主要方法没有执行;

我可能会在 IbftUtil.java 类中做某种引用错误,我试图通过引入该静态块来使事情成为单例。

请帮忙。

ibftUtil.java

static {

        try {

            /******************** Read Properties file.. ***************/ 

            LOGGER.println( LOG + " Loading properties file.." );

            InputStream inputStream = new FileInputStream( new File( FMS_PROPERTY_FILE ) );

            fmsProperties = new Properties();

            fmsProperties.load( inputStream );

            LOGGER.println( LOG + " Properties Loaded Successfully" );

            inputStream.close();

            LOGGER.println( LOG + " Reading properties values.. " );

            // Timer attributes..
            delayTime = Long.parseLong( fmsProperties.getProperty ( IBFT_DELAY_TIME ) );
            executionTime = Long.parseLong( fmsProperties.getProperty ( IBFT_EXECUTION_TIME ) );

            LOGGER.println( LOG + " IBFT DELAY & EXECUTION TIME = " + getDelayTime() + "," + getExecutionTime());

            // Key-store and certificate ..
            ibftReceiverCertificate = fmsProperties.getProperty( IBFT_RECEIVER_CERTIFICATE );
            ibftSenderKeyStore = fmsProperties.getProperty( IBFT_SENDER_KEY_STORE );
            ibftKeyStoreAlias = fmsProperties.getProperty( IBFT_KEY_STORE_ALIAS );

            // Key store credentials..
            ibftKeyStoreKey = fmsProperties.getProperty( IBFT_KEY_STORE_KEY );
            ibftKeyStorePath = fmsProperties.getProperty( IBFT_KEY_STORE_PASS );

            // Copy file path..
            ibftPaymentFiles = fmsProperties.getProperty( IBFT_PAYMENT_FILES );

            LOGGER.println( LOG + " Properties values read successfully!.. " );

            /******************** Database Connection.. ********************/

            LOGGER.println( LOG + " Connecting to Database" ); 

            connection = DatabaseConnection.getDatabaseConnection().getConnection( FMS_POST_SOURCE );
            connection.setAutoCommit( false );

            LOGGER.println( LOG + " FMS Database Connected.." );


        } catch ( Exception e ) {

            LOGGER.println( LOG + " Exception in Singleton block " + e );

        }

    }

TimerTask.java

class IbftFileTimerTask extends TimerTask implements IConstants {

    // Variables to hold to values returned from queries..
    private String fundBankId = EMPTY_STRING;
    private String fundId = EMPTY_STRING;
    private String fundBankAccountNo = EMPTY_STRING;
    private String productType = EMPTY_STRING;

    private String ibftDirectoryName = EMPTY_STRING;
    private String bankShortName = EMPTY_STRING;
    private String ibftCallingView = EMPTY_STRING;
    private String ibftFileName = EMPTY_STRING;
    private String amcShortName = EMPTY_STRING;
    private String cnicOrNtn = EMPTY_STRING;
    private String paymentRecordId = EMPTY_STRING;
    private String ibftDirectoryPath = EMPTY_STRING;

    // Variable for Hard-coded product type..
    private String fileProductType = EMPTY_STRING;

    // ArrayList for holding paymentRecords Processed..
    private ArrayList<String> paymentRecordIdList = null;

    // File Sequence counter for appending in file name..
    private int fileSequence = 0;

    // Class Object for Utility Class..
    private static IbftUtil util = new IbftUtil();

   /**
    * *******************************************************************************
    *                       EPM FILE ENCRYPTION - SCHEDULER
    * *******************************************************************************
    */

    public void run() {

        LOGGER.println( LOG + " Time = " + util.getSystemDate() );

        LOGGER.println( LOG + " IBFT Payment Files timer starts.." );

        try {

            executor();
        } 

        catch (Exception e) {

            LOGGER.println( LOG + " Exception=" + e.getMessage() );
        }

        LOGGER.println( LOG + " IBFT Payment Files timer Ends.." );
    }

@SuppressWarnings("static-access")
    public static void main ( String[] args ) throws SQLException {

        LOGGER.println( LOG + " ---- IBFT TIMER TASK ---- " );

        // Instantiate Timer Object
        Timer time = new Timer(); 

        // Class object
        IbftUtil util = new IbftUtil();

        // Instantiate EPMTimerTask class
        IbftFileTimerTask ibft = new IbftFileTimerTask(); 

        // Create Repetitively task for every 2 minutes
        time.schedule( ibft, util.delayTime, util.executionTime ); 

    }

标签: java

解决方案


推荐阅读