Search in sources :

Example 1 with JmsDistributeBolt

use of com.ds.retl.bolt.JmsDistributeBolt in project main by JohnPeng739.

the class JmsManager method createJmsBolt.

/**
 * 创建一个JMS数据持久化Bolt,用于存储中间数据
 *
 * @param supported 支持的JMS类型
 * @param config    初始化配置信息
 * @return 创建好的数据持久化器Bolt
 * @throws JMSException 创建过程中发生的异常
 */
public static BaseRichBolt createJmsBolt(Supported supported, JSONObject config) throws JMSException {
    if (config == null) {
        throw new NullPointerException("The Jms config is null.");
    }
    // TODO 根据destinations的不同内容创建JmsBolt或JmsDistributeBolt
    JSONArray destinations = config.getJSONArray("destinations");
    BaseRichBolt bolt;
    if (destinations.size() > 1) {
        // 创建JmsDistributeBolt
        bolt = new JmsDistributeBolt(destinations);
        ((JmsDistributeBolt) bolt).setJmsMultiProvider(createProvider(supported, config));
    } else {
        // 创建JmsBolt
        bolt = new JmsBolt();
        ((JmsBolt) bolt).setJmsProvider(createProvider(supported, config));
        ((JmsBolt) bolt).setJmsMessageProducer(new JmsMessageProducer() {

            @Override
            public Message toMessage(Session session, ITuple input) throws JMSException {
                // 从元组中读取json字段的值,然后作为一个TextMessage发送
                JSONObject managedJson = (JSONObject) input.getValueByField("managedJson");
                JSONObject data = (JSONObject) input.getValueByField("data");
                if (managedJson == null || data == null) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("The ManagedJson or Data is null.");
                    }
                    return null;
                }
                JSONObject json = new JSONObject();
                json.put("managedJson", managedJson);
                json.put("data", data);
                return session.createTextMessage(json.toJSONString());
            }
        });
    }
    return bolt;
}
Also used : JmsMessageProducer(org.apache.storm.jms.JmsMessageProducer) JmsBolt(org.apache.storm.jms.bolt.JmsBolt) Message(javax.jms.Message) JSONObject(com.alibaba.fastjson.JSONObject) ITuple(org.apache.storm.tuple.ITuple) JSONArray(com.alibaba.fastjson.JSONArray) JmsDistributeBolt(com.ds.retl.bolt.JmsDistributeBolt) JMSException(javax.jms.JMSException) BaseRichBolt(org.apache.storm.topology.base.BaseRichBolt) Session(javax.jms.Session)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 JmsDistributeBolt (com.ds.retl.bolt.JmsDistributeBolt)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 Session (javax.jms.Session)1 JmsMessageProducer (org.apache.storm.jms.JmsMessageProducer)1 JmsBolt (org.apache.storm.jms.bolt.JmsBolt)1 BaseRichBolt (org.apache.storm.topology.base.BaseRichBolt)1 ITuple (org.apache.storm.tuple.ITuple)1