分布式-信息方式-ActiveMQ构建应用
ActivemQ构建应用
Broker:相当于一个 ActiveMQ服务器实例
命令行启动参数示例如下:
1: activemq start:使用默认的 actived.xml来启动
2: activemq start xbean:file: ../onf/ actived-2.xml:使用指定的配置文件
来启动
3:如果不指定file,也就是 xbean: activemq-2.xml,那么xml必须在 classpath下面
用 ActiveMQ米构建Java应用
这里主要将用 Activemq broken作为独立的消息服务器来构建JAVA应用
ActiveMQ也支持在vm中通信基于嵌入式的 broker,能够无缝的集成其它Java应用
■嵌入式 Broker启动
1: Broker service启动 broker,示例如下:
BrokerService broker=new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();
package com.mq.test.activeMQ; import java.net.URI; import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class InnerBroker {
public static void main(String[] args) throws Exception {
BrokerService broker=new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start(); }
}
2: BrokerFactory启动 broker,示例如下:
String Uri ="properties:broker.properties";
BrokerService broker1 =BrokerFactory.createBroker(new URI(Uri));
broker1.addConnector("tcp://localhost:61616");
broker1.start();
broker.properties
useJmx=true
persistent=false
brokerName=cheese
package com.mq.test.activeMQ; import java.net.URI; import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class InnerBroker {
public static void main(String[] args) throws Exception {
String Uri ="properties:broker.properties";
BrokerService broker1 =BrokerFactory.createBroker(new URI(Uri));
broker1.addConnector("tcp://localhost:61616");
broker1.start(); }
}
3:利用Spring集成Broker,Spring的配置文件如下:
<bean id="broker" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop">
<property name="brokerName" value="myBroker" />
<property name="persistent" value="false"></property>
<property name="transportConnectorURIs" >
<list>
<value>tcp://localhost:61616</value>
</list>
</property>
</bean>
package com.mq.test.activeMQ; import java.net.URI; import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class InnerBroker {
public static void main(String[] args) throws Exception { ApplicationContext ctx =new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-autowire="byName" default-lazy-init="false"> <bean id="broker" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop">
<property name="brokerName" value="myBroker" />
<property name="persistent" value="false"></property>
<property name="transportConnectorURIs" >
<list>
<value>tcp://localhost:61616</value>
</list>
</property>
</bean> </beans>
或者配置 BrokerFactoryBean,示例如下:
<beans>
<bean id=broker class="org. apache. activemq. xbean. BrokerFactoryBean">
<Property name="config" value="resources/activemq-simple.xml"/>
<property name="start" value="true"/>
</bean>
</beans>
■ ActiveMQ的启动:
1:可以通过在应用程序中以编码的方式启动 broker,例如: broker. start(
如果需要启动多个 broker,那么需要为 broker设置一个名字。例如
BrokerService broker= new BrokerService();
broker.setName("fred")
broker.addConnector("tcp: //localhost: 61616")
broker.start
2:还可以通过 spring来启动,前面已经演示过了
分布式-信息方式-ActiveMQ构建应用的更多相关文章
- 分布式-信息方式-ActiveMQ的Message dispatch高级特性之(指针) Message cursors
Message dispatch高级特性之 Message cursors概述 ActiveMQ发送持久消息的典型处现方式是:当消息的消费者准备就绪时,消息发送系统把存储的 消息 ...
- 分布式-信息方式-ActiveMQ的Destination高级特性3
虚拟destination用来创建逻辑destination,客户端可以通过它来生产和消费消息,它会把消息映射到物理destination. ActiveMQ支持2种方式: 1:虚拟主题(Virtua ...
- 分布式-信息方式-ActiveMQ的Destination高级特性1
ActiveMQ的Destination高级特性 Destination高级特性----->Composite Destinations 组合队列Composite Destinations : ...
- 分布式-信息方式-ActiveMQ的集群
ActiveMQ的集群Queue consumer clusters ActiveMQ支持 Consumer对消息高可靠性的负载平衡消费,如果一个 Consumer死掉,该消 ...
- 分布式-信息方式-ActiveMQ静态网络连接的容错
容错的链接Failover Protocol 前面讲述的都是client配置链接到指定的 broker上.但是,如果 Broker的链接失败怎么办呢?此时, Client有两个选项:要么立刻死掉,要么 ...
- 分布式-信息方式-ActiveMQ的消息存储持久化
ActiveMQ的消息存储持久化■概述ActiveMQ不仅支持 persistent和 non-persistent两种方式,还支持消息的恢复( recovery)方式PTPQueue的存储是很简单的 ...
- 分布式-信息方式-ActiveMQ基础
ActiveMQ简介 ActiveMQ是什么ActiveMQ是Apache推出的,一款开源全支持JMS.1和J2EE1.4范的JMS Provider实现的信息中间件.(message oriente ...
- 分布式-信息方式-ActiveMQ的Destination高级特性2
使用filtered destinations,在xml配置如下: <destinationInterceptors> <virtualDestinationInterceptor& ...
- 分布式-信息方式-ActiveMQ的动态网络链接
ActiveMQ的动态网络链接多播协议 multicast ActiveMQ使用 Multicast协议将一个 Service和其他的 Broker的 Service连接起来,IPmulticast是 ...
随机推荐
- cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法
cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...
- js设计模式--发布订阅模式
前言 本系列文章主要根据<JavaScript设计模式与开发实践>整理而来,其中会加入了一些自己的思考.希望对大家有所帮助. 概念 发布-订阅模式又叫观察者模式,它定义对象间的一种一对多的 ...
- Spring Cloud Gateway真的有那么差吗?
动机 已经不止一次看到"Spring Cloud Gateway性能比Zuul更差"的言论了,不少人人云亦云,来问我,既然如此,那Spring官方还开发Spring Cloud G ...
- Java常见数据结构
HashMap深入浅出 HashMap数据结构 HashMap的本质就是一个数组加链表,数组默认长度是16,存储的元素达到总长度的75%就会扩容一倍.map.put(key,val),实际上就是根据h ...
- FluentValidation在C# WPF中的应用
原文:FluentValidation在C# WPF中的应用 一.简介 介绍FluentValidation的文章不少,零度编程的介绍我引用下:FluentValidation 是一个基于 .NET ...
- 一份非常完整、详细的MySQL规范
一.数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名 ...
- centos查看实时网络带宽占用情况方法【转】
Linux中查看网卡流量工具有iptraf.iftop以及nethogs等,iftop可以用来监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等. centos安装iftop的命令如下: ...
- 帝国cms 遍历某个父栏目下所有的子栏目
[e:loop={"select * from phome_enewsclass where bclassid in (2) order by classid asc",0,24, ...
- TensorFlow入门——安装
由于实验室新配了电脑,旧的电脑就淘汰下来不用,闲来无事,就讲旧的电脑作为个人的工作站来使用. 由于在旧电脑上安装的是Ubuntu 16.04 64bit系统,系统自带的是Python 2.7,版本选择 ...
- Java缓存要点
1.缓存一般是这样的:先查缓存,查不到就查DB,如果DB查不到就结束,DB查到了就写入缓存. 如果用户一直在大量地查询不存在的数据,则所有的请求都会落到DB,而且没有数据写入缓存. 解决方法:把查不到 ...