GithubHelp home page GithubHelp logo

garyfub / mumu-activemq Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mumudemo/mumu-activemq

0.0 2.0 0.0 39 KB

mumu-activemq是一个对老牌mq消息中间件的学习和测试项目,本人通过这个项目来熟悉activemq的消息发送流程和消息接受流程。activemq支持各种消息协议tcp、http、udp等。activemq遵循jms开发协议,支持队列、主题两种消息模式。而且activemq已经内嵌到spring中去,当使用spring框架的时候很容易的就将activemq集成到项目中去。

License: Apache License 2.0

Java 100.00%

mumu-activemq's Introduction

mumu-activemq 消息中间件

License Maven Central Build Status OpenTracing-1.0 Badge

mumu-activemq是一个对老牌mq消息中间件的学习和测试项目,本人通过这个项目来熟悉activemq的消息发送流程和消息接受流程。activemq支持各种消息协议tcp、http、udp等。activemq遵循jms开发协议,支持队列、主题两种消息模式。而且activemq已经内嵌到spring中去,当使用spring框架的时候很容易的就将activemq集成到项目中去。

队列消息模式

队列一般用于点对点的消息发送和接受。消息发送者将消息发送到队列,接受者从队列中获取消息。 队列消息模式

代码演示

消息发送

public static void sendMessage() {
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
				"failover:(tcp://192.168.11.25:61616)");
		try {
			Connection connection = activeMQConnectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			Queue queue = session.createQueue("lgan");
			MessageProducer producer = session.createProducer(queue);

			TextMessage message = session.createTextMessage("lovecws"+new Date());
			producer.send(message);
			session.commit();
			connection.stop();
			connection.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

消息接受

public static void receiveMessage() {
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
				"failover:(tcp://192.168.11.25:61616)");
		try {
			Connection connection = activeMQConnectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

			Queue queue = session.createQueue("lgan");
			MessageConsumer consumer = session.createConsumer(queue);
			Message message = consumer.receiveNoWait();
			if (message != null) {
				System.out.println(message);
				String object = ((ActiveMQTextMessage) message).getText();
				System.out.println(object);
			} else {
				System.out.println("message not found");
			}
			session.commit();
			connection.stop();
			connection.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

主题消息模式

主题消息模式主要用于消息的订阅。消息发送者将消息发送到主题,然后订阅该主题的消息接受者都可以接受该消息。 队列消息模式

相关阅读

深入浅出JMS(一)–JMS基本概念
深入浅出JMS(二)–ActiveMQ简单介绍以及安装
深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例

联系方式

以上观点纯属个人看法,如有不同,欢迎指正。
email:[email protected]
github:https://github.com/babymm

mumu-activemq's People

Contributors

babymm avatar

Watchers

James Cloos avatar  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.