Code Monkey home page Code Monkey logo

xm-message's Introduction

#XM-Message 利用redis做消息队列,并包含失败重发机制。

此消息队列使用的都是批量读/写入redis。

亲测,redis以一条消息为单位进行存取,效率为2000/s

100条为单位,效率为10W/s

        pipeline.rpush(MSG_KEY, message);

        if(pipCount % 100 == 0){
            pipeline.sync();
        }

1.生产者使用方式: XMProducer producer = new XMProducer(jedisPool.getResource(), "test"); System.out.println(System.currentTimeMillis());

2.消费者使用方式

XMConsumer consumer = new XMConsumer(jedisPool.getResource(), "test");
		while(true) {
			consumer.consume(true, 1000, new XMReadMessageImp() {
				@Override
				public void onMessage(List<Object> message) {
					for (Object str : message) {
						System.out.println((String) str);
					}
				}
			});
		}

3.消息失败存入失败队列

		XMFailureProducer producer = new XMFailureProducer(jedisPool.getResource(), "test");
			producer.publish("failure:message: " + i);

4.从失败队列中读取重试

XMFailureConsumer consumer = new XMFailureConsumer(jedisPool, "test");
		while(true) {
			consumer.consume(true, 1000, new XMReadMessageImp() {
				@Override
				public void onMessage(List<Object> message) {
					for(Object object:message) {
						MsgFailureInfo msgFailureInfo = (MsgFailureInfo)object;
						String key = msgFailureInfo.getKey();
						if(MAX_FAILURE_COUNT.equals(msgFailureInfo.getKey())) {
							//you can do something in there.
							consumer.delFailureMessage(key);
							System.out.println("The number of failures has reached its maximum");
						}else {
							//add the failure count
							consumer.addFailureCount(key, consumer.getFailureCountForKey(key));
						}
					}
				}
			});
		}

xm-message's People

Contributors

wenyiwu avatar

Watchers

Andy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.