1:下载:apache-activemq-5.14.0-bin.tar.gz
http://activemq.apache.org/activemq-5140-release.html
2:安装activemq
1:gz文件拷贝到/usr/local/目录
mv apache-activemq-5.14.0-bin.tar.gz /usr/local
2:解压启动
tar -zxvf apache-activemq-5.14.0-bin.tar.gz
3:进入到bin目录
cd apache-activemq-5.14.0/bin
4:启动activemq
./activemq start
5:停止ativemq
./activemq stop
3:开启防火墙端口
1:如果使用了云服务器需要先开启8161(web管理页面端口)、61616(activemq服务监控端口) 两个端口
2:打开linux防火墙端口
/sbin/iptables -I INPUT -p tcp --dport 8161 -j ACCEPT&&/etc/init.d/iptables save&&service iptables restart&&/etc/init.d/iptables status
/sbin/iptables -I INPUT -p tcp --dport 61616 -j ACCEPT&&/etc/init.d/iptables save&&service iptables restart&&/etc/init.d/iptables status
4:打开web管理页面
http://192.168.1.128:8161/admin 默认用户名密码 admin/admin
5:activemq的安全配置(影响服务端生产者和消费者的安全校验)
1:打开conf/activemq.xml
vi conf/activemq.xml
2:在broker标签间-->shutdownHooks标签后增加如下标签
1:<simpleAuthenticationPlugin>节点通过子节点定义用户、组信息
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="scott" password="13871500871" groups="users,admins"/>
<authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
备注: 其中groups信息用于验证授权,用户信息可以从conf/credentials.properties中通过${}进行获得,内容如下:
activemq.username=scott
activemq.password=13871500871
2:<jaasAuthenticationPlugin"/>节点通过Java Authentication Authorization Service进行用户验证及授权操作
<plugins>
<jaasAuthenticationPlugin configuration="activemq-domain"/>
</plugins>
备注:其中configuration=“activemq-domain”中的activemq-domain要与conf/login.config中节点名称对应,内容如下
activemq-domain {
org.apache.activemq.jaas.PropertiesLoginModule required
org.apache.activemq.jaas.properties.user="users.properties"
org.apache.activemq.jaas.properties.group="groups.properties"
reload=true;
};
3:<authorizationPlugin>节点用于通过jaas进行用户的授权
<plugins>
<authorizationEntry queue=">" read="admins" write="admins" admin="admins"/>
<authorizationEntry topic="USERS.>" read="users" write="users" admin="users"/>
</plugins>
read :destination中消息的浏览及消费
write :destination中消息的创建
admin :destiantion中最高权限,任意消息的管理
消息有两种queue和topic,其中read、write、admin用户指定用户组,其中conf/groups.properties中文件内容如下
admins=admin
users=admin,develop,sslclient
tempDestinationAdmins=system,user,sslclient,client,broker1,broker2
guests=guest
4:服务端代码
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("scott","13871500871","tcp://192.168.1.128:61616");
6:activemq-admin的访问配置
1:配置ActiveMQ的web Console控制台端口:
1:编辑jetty.xml
vi conf/jetty.xml
2:修改端口配置,如下为默认信息
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
<!-- the default port number for the web console -->
<property name="host" value="0.0.0.0"/>
<property name="port" value="8161"/>
</bean>
2:ActiveMQ的web Console控制台用户名密码配置:
1:编辑jetty-realm.xml
vi conf/jetty-realm.properties
2:修改用户配置,如下
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
scott: 13871500871, user
7:activemq持久化配置
1:增加jar包
commons-dbcp2.jar
mysql-connector-Java-5.1.30-bin.jar
2:打开conf/activemq.xml
vi conf/activemq.xml
3:ActiveMQ支持三种持久化策略:AMQ、KAHADB(默认)、JDBC
AMQ 它是一种文件存储形式,具有写入快、容易恢复的特点,采用这种方式持久化消息会被存储在一个个文件 中,每个文件默认大小为32MB,如果一条消息超过32MB,那么这个值就需要设大。当一个文件中所有的消息被“消费”掉了,那么这文件会被置成“删除”标志,并且在下一个清除开始时被删除掉。
KAHADB,相比AMQ来説,KAHADB速度没有AMQ快,可是KAHADB具有极强的垂直和横向扩展能力,恢复时间比AMQ还要短,因此从5.4版后ActiveMQ默认使用KAHADB作为其持久化存储。而且在作MQ的集群时使用KAHADB可以做到Cluster+Master Slave的这样的完美高可用集群方案。
JDBC,即ActiveMQ默认可以支持把数据持久化到DB中,如:MYSQL、ORACLE等。
4:修改配置
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/><!-- (默认) -->
<jdbcPersistenceAdapter dataSource="#derby-ds"/>
</persistenceAdapter>
5:在配置文件末尾增加
<bean id="derby-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.253.4:3306/wqh?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="13871500871"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
注:class="org.apache.commons.dbcp2.BasicDataSource"之前的版本为org.apache.commons.dbcp.BasicDataSource,引用commons-dbcp.jar即可
8:activemq消息过滤
JMS规范中的消息类型包括TextMessage、MapMessage、ObjectMessage、BytesMessage、和StreamMessage等五种,他们都继承Message
Message提供基本的属性设置声明,如:,
void setIntProperty(String name, int value) throws JMSException;
void setLongProperty(String name, long value) throws JMSException;
void setFloatProperty(String name, float value) throws JMSException;
void setDoubleProperty(String name, double value) throws JMSException;
void setStringProperty(String name, String value) throws JMSException;
等等,
文本消息:TextMessage
void setText(String string) throws JMSException;
String getText() throws JMSException;
Map消息:MapMessage
void setInt(String name, int value) throws JMSException;
void setLong(String name, long value) throws JMSException;
void setFloat(String name, float value) throws JMSException;
void setDouble(String name, double value) throws JMSException;
void setString(String name, String value) throws JMSException;
//get系列省略
Object消息:ObjectMessage
void setObject(Serializable object) throws JMSException;
Serializable getObject() throws JMSException;
Byte消息:BytesMessage
int readInt() throws JMSException;
void writeInt(int value) throws JMSException;
//read系列和write系列
Stream消息:StreamMessage
int readInt() throws JMSException;
void writeInt(int value) throws JMSException;
//read系列和write系列
生产者:
MapMessage mapMessage = this.session.createMapMessage();
mapMessage.setString("name", "张三");
mapMessage.setInt("age", 20);
// 设置用于消息过滤器的条件
mapMessage.setStringProperty("name", "张三");
mapMessage.setIntProperty("age", 20);
mapMessage.setStringProperty("color", "bule");
producer.send(message);
消费者:
Destination destination = session.createTopic(queueName);
MessageConsumer consumer = session.createConsumer(destination,"age>10 and score<30");
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
//TextMessage textMessage = (TextMessage)message;
MapMessage textMessage = (MapMessage)message;
System.out.println("消费者2:" + textMessage);
}
});
关于消息选择器
MessageConsumer createConsumer( Destination destination, String messageSelector )
MessageConsumer createConsumer( Destination destination, String messageSelector, boolean noLocal )
其中,messageSelector为消息选择器;
noLocal标志默认为false,当设置为true时,限制消费者只能接收和自己相同的连接(Connection)所发布的消息,此标志只适用于主题,不适用于队列。