Search in sources :

Example 1 with TopicAndFilter

use of com.alibaba.middleware.race.mom.util.TopicAndFilter in project alibaba-mom by younfor.

the class Broker method processConsumer.

/**
 * 收到消费者的订阅关系,并创建队列
 * @param consumerRequestInfo
 * @param ctx
 */
public void processConsumer(InfoBodyConsumer consumerRequestInfo, final ChannelHandlerContext ctx) {
    String topic = consumerRequestInfo.getTopic();
    String groupid = consumerRequestInfo.getGroupId();
    Map<String, String> filter = consumerRequestInfo.getFilterMap();
    TopicAndFilter topicAndFilter = new TopicAndFilter(topic, filter);
    Channel consumer = ctx.channel();
    logger.error("消费者:" + consumer.hashCode() + "发起订阅" + consumerRequestInfo);
    ConsumerGroup.addConsumer(consumer, groupid, topicAndFilter);
// 
}
Also used : TopicAndFilter(com.alibaba.middleware.race.mom.util.TopicAndFilter) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel)

Example 2 with TopicAndFilter

use of com.alibaba.middleware.race.mom.util.TopicAndFilter in project alibaba-mom by younfor.

the class Recover method makeOffsets.

/**
 * 集群组 进度重叠交叉等,对其取并集,避免从磁盘重复读取消息
 * 遍历所有恢复的Offset,做其它恢复的准备和恢复消息去重
 * @param offsets
 * @return
 */
public List<Offset> makeOffsets(ArrayList<Offset> offsets) {
    HashMap<TopicAndFilter, Offset> makedOffsets = new HashMap<>();
    HashMap<TopicAndFilter, Map<Integer, Integer>> /*maxOffset*/
    producerGroupRecoverInfos = new HashMap<>();
    Offset makedoffset;
    TopicAndFilter topicAndFilter;
    for (Offset offset : offsets) {
        topicAndFilter = new TopicAndFilter(offset.getTopic(), offset.getFilter());
        System.out.println(offset);
        /*
			 * 恢复存储进度关系
			 */
        if (producerGroupRecoverInfos.containsKey(topicAndFilter)) {
            producerGroupRecoverInfos.get(topicAndFilter).put(offset.getQueueIndex(), offset.getMaxOffset());
        } else {
            Map<Integer, Integer> /*maxOffset*/
            maxoffsets = new HashMap<>();
            maxoffsets.put(offset.getQueueIndex(), offset.getMaxOffset());
            producerGroupRecoverInfos.put(topicAndFilter, maxoffsets);
        }
        if (makedOffsets.containsKey(topicAndFilter)) {
            makedoffset = makedOffsets.get(topicAndFilter);
            if (offset.getCurrentoffset() < makedoffset.getCurrentoffset()) {
                makedoffset.setCurrentoffset(offset.getCurrentoffset());
            }
            if (offset.getMaxOffset() > makedoffset.getMaxOffset()) {
                makedoffset.setMaxOffset(offset.getMaxOffset());
            }
        } else {
            makedOffsets.put(topicAndFilter, offset);
        }
    }
    ProducerGroup.recover(producerGroupRecoverInfos);
    return Arrays.asList(makedOffsets.values().toArray(new Offset[0]));
}
Also used : TopicAndFilter(com.alibaba.middleware.race.mom.util.TopicAndFilter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TopicAndFilter

use of com.alibaba.middleware.race.mom.util.TopicAndFilter in project alibaba-mom by younfor.

the class ConsumerGroup method getGroup.

public static ConsumerGroup getGroup(TopicAndFilter topicAndFilter, String groupId) {
    Map<String, ConsumerGroup> groups = getGroups(topicAndFilter);
    Map<String, ConsumerGroup> groupsNoFilter = getGroups(new TopicAndFilter(topicAndFilter.getTopic(), null));
    ConsumerGroup group;
    if (groupsNoFilter == null && groups == null) {
        return null;
    } else if (groups != null && (group = groups.get(groupId)) != null) {
        return group;
    } else if (groupsNoFilter != null) {
        return groupsNoFilter.get(groupId);
    } else
        return null;
}
Also used : TopicAndFilter(com.alibaba.middleware.race.mom.util.TopicAndFilter)

Example 4 with TopicAndFilter

use of com.alibaba.middleware.race.mom.util.TopicAndFilter in project alibaba-mom by younfor.

the class ConsumerGroup method getNeedSendGroups.

/**
 * 取得filter 和null两种needSend订阅集群组
 * @param topicAndFilter
 * @return
 */
public static ConcurrentHashMap<String, ConsumerGroup> getNeedSendGroups(TopicAndFilter topicAndFilter) {
    ConcurrentHashMap<String, ConsumerGroup> groups = getGroups(topicAndFilter);
    TopicAndFilter topicNoFilter = new TopicAndFilter(topicAndFilter.getTopic(), null);
    ConcurrentHashMap<String, ConsumerGroup> noFilterGroups = getGroups(topicNoFilter);
    if (groups != null && noFilterGroups != null) {
        groups.putAll(getGroups(topicNoFilter));
    } else if (noFilterGroups != null) {
        groups = noFilterGroups;
    }
    // }
    return groups;
}
Also used : TopicAndFilter(com.alibaba.middleware.race.mom.util.TopicAndFilter)

Example 5 with TopicAndFilter

use of com.alibaba.middleware.race.mom.util.TopicAndFilter in project alibaba-mom by younfor.

the class DefaultConsumer method prepare.

@Override
public void prepare() {
    SIP = System.getProperty("SIP");
    if (SIP == null)
        SIP = "127.0.0.1";
    System.out.println("consumer connect:" + System.getProperty("SIP"));
    group = new NioEventLoopGroup();
    try {
        bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel channel) throws Exception {
                channel.pipeline().addLast(new RpcEncoder()).addLast(new RpcDecoder()).addLast(new SimpleChannelInboundHandler<Object>() {

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                        connect();
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
                        logger.error("消费者异常关闭:");
                        e.printStackTrace();
                        ctx.close();
                    }

                    @Override
                    protected void channelRead0(final ChannelHandlerContext ctx, Object info) throws Exception {
                        logger.debug("client receive msg");
                        Message msg = (Message) info;
                        // 返回ACK
                        final ConsumeResult consumeResult = listener.onMessage(msg);
                        // 设置谁的 ack
                        consumeResult.setMsgId(msg.getMsgId());
                        /*
										 *设置 consumeResult来源
										 */
                        consumeResult.setGroupId(groupId);
                        consumeResult.setTopicAndFilter(new TopicAndFilter(topic, filterMap));
                        // if(Math.random()>0.95){
                        // new Thread(new Runnable(){
                        // 
                        // @Override
                        // public void run() {
                        // // TODO Auto-generated method stub
                        // try {
                        // Thread.sleep(10000);
                        // 
                        // ctx.writeAndFlush(consumeResult);
                        // } catch (InterruptedException e) {
                        // // TODO Auto-generated catch block
                        // e.printStackTrace();
                        // }
                        // }
                        // 
                        // }).start();;
                        // }else
                        ctx.writeAndFlush(consumeResult);
                    }
                });
            }
        });
        connect();
    } catch (InterruptedException e) {
        logger.error("消费者抛出异常  " + e.getMessage());
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) TopicAndFilter(com.alibaba.middleware.race.mom.util.TopicAndFilter) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) RpcEncoder(com.alibaba.middleware.race.mom.serializer.RpcEncoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RpcDecoder(com.alibaba.middleware.race.mom.serializer.RpcDecoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

TopicAndFilter (com.alibaba.middleware.race.mom.util.TopicAndFilter)7 Message (com.alibaba.middleware.race.mom.Message)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 RpcDecoder (com.alibaba.middleware.race.mom.serializer.RpcDecoder)1 RpcEncoder (com.alibaba.middleware.race.mom.serializer.RpcEncoder)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1