第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Storm框架:如何消費(fèi)RabbitMq消息(代碼案例)

標(biāo)簽:
Java

1、定义拓扑topology

public class MessageTopology {    public static void main(String[] args) throws Exception {        //组装topology
        TopologyBuilder topologyBuilder = new TopologyBuilder();
        topologyBuilder.setSpout("RabbitmqSpout", new RabbitmqSpout());
        topologyBuilder.setBolt("FilterBolt", new FilterBolt()).shuffleGrouping("RabbitmqSpout");

        Config conf = new Config ();        try {            if (args.length > 0) {
                StormSubmitter.submitTopology(args[0], conf, topologyBuilder.createTopology());
            } else {
                LocalCluster localCluster = new LocalCluster();
                localCluster.submitTopology("messageTopology", conf, topologyBuilder.createTopology());
            }
        } catch (AlreadyAliveException e) {
            e.printStackTrace();
        }
    }
}

2、定义数据源RabbitmqSpout

RabbitmqSpout继承自org.apache.storm.topology.IRichSpout接口,实现对应的方法:open(),close(),activate(),deactivate(),nextTuple(),ack(),fail()。

unconfirmedMap对象存储了MQ所有发射出去等待确认的消息唯一标识deliveryTag,当storm系统回调ack、fail方法后进行MQ消息的成功确认或失败重回队列操作(Storm系统回调方法会在bolt操作中主动调用ack、fail方法时触发)。

public class RabbitmqSpout implements IRichSpout {    private final Logger LOGGER = LoggerFactory.getLogger(RabbitmqSpout.class);    private Map map;    private TopologyContext topologyContext;    private SpoutOutputCollector spoutOutputCollector;    private Connection connection;    private Channel channel;    private static final String QUEUE_NAME = "message_queue";    private final Map<String, Long> unconfirmedMap = Collections.synchronizedMap(new HashMap<String, Long>());    //连接mq服务
    private void connect() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/");

        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    }    @Override
    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {        this.map = map;        this.topologyContext = topologyContext;        this.spoutOutputCollector = spoutOutputCollector;        try {            this.connect();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }    @Override
    public void close() {        try {
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }    @Override
    public void nextTuple() {        try {
            GetResponse response = channel.basicGet(QUEUE_NAME, false);            if (response == null) {
                Utils.sleep(3000);
            } else {
                AMQP.BasicProperties props = response.getProps();
                String messageId = UUID.randomUUID().toString();
                Long deliveryTag = response.getEnvelope().getDeliveryTag();
                String body = new String(response.getBody());

                unconfirmedMap.put(messageId, deliveryTag);
                LOGGER.info("RabbitmqSpout: {}, {}, {}, {}", body, messageId, deliveryTag, props);                this.spoutOutputCollector.emit(new Values(body), messageId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    @Override
    public void ack(Object o) {
        String messageId = o.toString();
        Long deliveryTag = unconfirmedMap.get(messageId);
        LOGGER.info("ack: {}, {}, {}\n\n", messageId, deliveryTag, unconfirmedMap.size());        try {
            unconfirmedMap.remove(messageId);
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    @Override
    public void fail(Object o) {
        String messageId = o.toString();
        Long deliveryTag = unconfirmedMap.get(messageId);
        LOGGER.info("fail: {}, {}, {}\n\n", messageId, deliveryTag, unconfirmedMap.size());        try {
            unconfirmedMap.remove(messageId);
            channel.basicNack(deliveryTag, false, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("body"));
    }    @Override
    public Map<String, Object> getComponentConfiguration() {        return null;
    }    
    @Override
    public void activate() {

    }    @Override
    public void deactivate() {

    }
}

3、定义数据流处理FilterBolt

public class FilterBolt implements IRichBolt {    private final Logger LOGGER = LoggerFactory.getLogger(FilterBolt.class);    private Map map;    private TopologyContext topologyContext;    private OutputCollector outputCollector;    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {        this.map = map;        this.topologyContext = topologyContext;        this.outputCollector = outputCollector;
    }    @Override
    public void execute(Tuple tuple) {
        String value = tuple.getStringByField("body");

        LOGGER.info("FilterBolt:{}", value);
        outputCollector.ack(tuple);
    }    
    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("body"));
    }    @Override
    public Map<String, Object> getComponentConfiguration() {        return null;
    }    
    @Override
    public void cleanup() {

    }
}

Hey, show me the code!

原文出处:https://www.cnblogs.com/gouyg/p/java_storm_rabbitmq_example.html  

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 1
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消