activeMq之hello(java)

本文介绍如何使用ActiveMQ消息队列解决第三方响应慢的问题,包括安装配置、创建项目、发送与接收消息等步骤。通过示例代码展示了如何在Java项目中集成ActiveMQ。

消息队列activeMq,   节省响应时间,解决了第三方响应时间长的问题让其他客户可以继续访问,

安装activeMq

apache-activemq-5.14.0-bin\apache-activemq-5.14.0\bin\win64\activeMq.bat

创建一个maven java project

在浏览器中访问路径  http://localhost:8161/        登录名admin  密码为admin

 

 

1.pom.xml文件

<dependencies>
  <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.14.0</version>
</dependency>
  </dependencies>
  <build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

  

 生产任务

    public static void main(String[] args) throws JMSException {
		
		//连接工厂
		ConnectionFactory factory = new ActiveMQConnectionFactory();
		//获取一个连接
		Connection connection = factory.createConnection();
		//建立会话
		Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
		//创建队列化话题对象
		Queue queue = session.createQueue("hello");
		MessageProducer producer = session.createProducer(queue);
		for (int i = 0; i < 10; i++) {
			producer.send(session.createTextMessage("ActiveMQ"+i));
		}
		session.commit();
	}

  产生了十条任务

消费(处理业务)

public static void main(String[] args) throws Exception {
		//连接工厂
		ConnectionFactory factory = new ActiveMQConnectionFactory();
		//获取一个连接
		Connection connection = factory.createConnection();
		connection.start();
		//建立会话
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//创建队列化话题对象
		Queue queue = session.createQueue("hello");
		MessageConsumer producer = session.createConsumer(queue);
		while(true){
			//接收消息
			TextMessage receive = (TextMessage) producer.receive();
			if(receive!=null){
				System.out.println(receive.getText());
			}
		}
	}

  

 

转载于:https://www.cnblogs.com/fjkgrbk/p/activeMq.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值