Storm bolt/spout生命周期

在通过main函数提交topo之前,将storm相关的配置信息通过yaml文件对应类传入到bolt中,在某一个数据库连接池的公共类中同样需要相关的配置信息,因为业务和测试需求的便捷性,便将相关配置也放入了yaml中。起初是在main函数里面进行该公共类的初始化,即将相关的config信息传入,在单bolt的情况下测试没有问题,便发布到服务器并配置多个bolt,由此就引发了一个空指针的异常。

后来在了解了storm 中bolt/spout的生命周期后,才理解了为什么会出现空指针。

The lifecycle of a bolt or spout is as follows: 
1. Created on client side (from where you submit the topology) and serialized using Java serialization 
2. Serialized component is sent to all the tasks 
3. Each task executing that component deserializes the component 
4. The task calls "prepare" (for bolts) or "open" (for spouts) on the component before it starts executing. 
So if you need to do something like connect to a database, you should do that in the "prepare" or "open" method. 


出现这个问题主要是对一个spout/bolt的生命周期不是很了解导致, 一般来说spout/bolt的生命周期如下: 
1.在提交了一个topology之后(并不一定是在nimbus所在的机器), 创建spout/bolt实例(spout/bolt在storm中统称为component)并进行序列化. 
2.将序列化的component发送给所有的任务所在的机器 
3.在每一个任务上反序列化component. 
4.在开始执行任务之前, 先执行component的初始化方法(bolt是prepare, spout是open). 
因此component的初始化操作应该在prepare/open方法中进行, 而不是在实例化component的时候进行,特别是类似于连接数据库等操作。

前面提到的空指针问题,正是因为数据库连接池的公共类的操作,并没有进入spout/bolt的生命周期导致的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值