Yes, that works! thinks :)



------------------ ???????? ------------------
??????: "sunil yadav"<raosun...@gmail.com>;
????????: 2018??12??29??(??????) ????10:38
??????: "user"<user@storm.apache.org>;

????: Re: How to keep information when submit topology



Keep it in config and access it in prepare/init method of your bolt. Only thing 
to ensure is the object needs to be serilizable.


On Fri, Dec 28, 2018 at 9:12 PM ChenBo <mrche...@qq.com> wrote:

Hi All, I use storm-core  1.2.2, there is my problem.


I keep some information in Class static field before submit topology to Storm. 
But after I found when topology run, all those information lost.


I think maybe it's because all class will be reload after submit. So Is there 
anyway to store some information before submit topology and also can get back 
those infor when topolog run?



And there is my code.


/**
 * the class is used to init mysql connection pool
 */public class DataBaseManger {
    private static final Logger logger = 
LoggerFactory.getLogger(DataBaseManger.class);
    private static BoneCP comboPooledDataSource;

    public static boolean init(String url, String user, String password) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            logger.error(e.getMessage());
            return false;
        }

        try {
            BoneCPConfig config = new BoneCPConfig();
            config.setJdbcUrl(url);
            config.setUsername(user);
            config.setPassword(password);
            config.setMinConnectionsPerPartition(5);
            config.setMaxConnectionsPerPartition(15);
            config.setPartitionCount(1);
            comboPooledDataSource = new BoneCP(config);
        } catch (SQLException e) {
            logger.error(e.getMessage());
            return false;
        }
        
        return true;
    }

    public static synchronized Connection getConnection() throws SQLException {
        return comboPooledDataSource.getConnection();
    }
}


/**
 * submit topology, and before that use local information to init the mysql 
connection pool
 */
    public static void main(String[] args) throws Exception {
        LocalConfig localConfig = new LocalConfig(args);

        DataBaseManger.init(LocalConfig.getDbUrl(), LocalConfig.getDbUser(), 
LocalConfig.getDbPw());

        StormTopology topology = createTopology(localConfig);
        Config config = getTopologyConfig(localConfig);

        StormSubmitter.submitTopology(localConfig.getTopologyName(), config, 
topology);
    }




In Blot use DataBaseManger.getConnection() to get connection. But it's get 
NullPointException. that turns out comboPooledDataSource is null.
So I am wounder is there anyway to keep mysql connection information after 
submit topology?





Any help is appreciated. Thanks, 
ChenBo

Reply via email to