1.配置jdk环境(Linux)
一般在普通用户下的 ~/.bash_profile 或者在 /etc/profile 最后添加如下语句:
export JAVA_HOME=/opt/app/jdk1.8.0_162/
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
2.配置文件
activemq.xml
ActiveMQ的主配置文件,其中有部分配置有所修改以适应我的程序。
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- START SNIPPET: example -->
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:${activemq.conf}/credentials.properties</value>
</property>
</bean>
<!-- Allows accessing the server log -->
<bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"
lazy-init="false" scope="singleton"
init-method="start" destroy-method="stop">
</bean>
<!--
The <broker> element is used to configure the ActiveMQ broker.
-->
<!-- brokerName 设置broker的名字,默认是localhost,注意在网络上必须是唯一的。
dataDirectory 设置持久化文件目录,默认为activemq的apache-activemq-5.11.1/data目录。
-->
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
<destinationPolicy>
<policyMap>
<policyEntries>
<!-- 这里使用通配符">",用于递归地匹配从这个名称开始的任何目的地 -->
<policyEntry topic=">" >
<!-- The constantPendingMessageLimitStrategy is used to prevent
slow topic consumers to block producers and affect other consumers
by limiting the number of messages that are retained
For more information, see:
http://activemq.apache.org/slow-consumer-handling.html
-->
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
<!-- 我自己添加的一个policyEntry(DLQ) -->
<policyEntry queue=">" >
<deadLetterStrategy>
<individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true"/>
</deadLetterStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<!--
The managementContext is used to configure how ActiveMQ is exposed in
JMX. By default, ActiveMQ uses the MBean server that is started by
the JVM. For more information, see:
http://activemq.apache.org/jmx.html
-->
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<!--
Configure message persistence for the broker. The default persistence
mechanism is the KahaDB store (identified by the kahaDB tag).
For more information, see:
http://activemq.apache.org/persistence.html
-->
<!--
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/>
</persistenceAdapter>
-->
<!-- 这个persistenceAdapter是我添加的,使用MySQL来持久化存储 -->
<persistenceAdapter>
<jdbcPersistenceAdapter dataSource="#MySQL-DS"/>
</persistenceAdapter>
<!--
The systemUsage controls the maximum amount of space the broker will
use before disabling caching and/or slowing down producers. For more information, see:
http://activemq.apache.org/producer-flow-control.html
-->
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage percentOfJvmHeap="70" />
</memoryUsage>
<storeUsage>
<storeUsage limit="100 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="50 gb"/>
</tempUsage>
</systemUsage>
</systemUsage>
<!--
The transport connectors expose ActiveMQ over a given protocol to
clients and other brokers. For more information, see:
http://activemq.apache.org/configuring-transports.html
-->
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<!-- The transport connectors ActiveMQ will listen to following transportConnector -->
<!-- 注释掉其他transportConnector,只配置openwire,允许其他进程来访问本机的所有地址上的61616端口。以及一些链接属性的配置 -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=3000&wireFormat.maxFrameSize=209715200&wireFormat.maxInactivityDuration=600000"/>
<!--
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=5000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=5000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=5000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=5000&wireFormat.maxFrameSize=104857600"/>
-->
</transportConnectors>
<!-- destroy the spring context on shutdown to stop jetty -->
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>
</broker>
<!-- 这个bean("MySQL-DS") 就是前面添加的persistenceAdapter("MySQL-DS")的具体消息持久化方式,如jdbc的参数,属性等-->
<bean id="MySQL-DS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true&autoReconnect=true&failOverReadOnly=false&socketTimeout=5000"/>
<property name="username" value="mquser"/>
<property name="password" value="Inteplay007"/>
<!--property name="maxActive" value="350"/-->
<property name="poolPreparedStatements" value="true"/>
</bean>
<!--
Enable web consoles, REST and Ajax APIs and demos
The web consoles requires by default login, you can disable this in the jetty.xml file
Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
-->
<!-- 代表从 jetty.xml文件中导入其他配置。 -->
<import resource="jetty.xml"/>
</beans>
<!-- END SNIPPET: example -->
通配符
.
用于在路径中分隔名称*
用于匹配路径中的任何名称>
用于递归地匹配从这个名称开始的任何目的地
关于通配符详细参见:http://activemq.apache.org/wildcards.html
jetty-realm.properties
定义可以访问web(console,demo等)的用户密码[角色]。
一般要修改默认的密码,或者防火墙关闭8161端口禁止访问。
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: user, user
jmx.access 和 jmx.password
配置jmx访问的账户密码
3.启动脚本 activemq
位于:apache-activemq-5.11.1/bin/
3.1 INIT INFO 系统服务脚本头
activemq通过命令 activemq start 启动之后,以系统服务的方式在后台运行。
以下是脚本的开头,这个是系统服务系统的方式
#!/bin/sh
### BEGIN INIT INFO
# Provides: activemq
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 6
# Short-Description: Starts ActiveMQ
# Description: Starts ActiveMQ Message Broker Server
### END INIT INFO
定义两个变量,分别记录命令行的所有参数和执行选项。
# ------------------------------------------------------------------------
# IMPROVED DEBUGGING (execute with bash -x)
# export PS4=' ${BASH_SOURCE}:${LINENO}(${FUNCNAME[0]}) '
#
# Backup invocation parameters
COMMANDLINE_ARGS="$@"
EXEC_OPTION=""
3.2 几个帮助函数
setCurrentUser()用于将当前用户赋值给CUSER变量,以便脚本里使用。
# ------------------------------------------------------------------------
# HELPERS
# a simple helper to get the current user
setCurrentUser(){
CUSER="`whoami 2>/dev/null`"
# Solaris hack
if [ ! $? -eq 0 ]; then
CUSER="`/usr/ucb/whoami 2>/dev/null`"
fi
}
pathCanonical()用于获得规范的路径,摒除系统的差异。
# get a canonical path, macosx and slowlaris does not support radlink -f :-)
pathCanonical() {
local dst="${1}"
while [ -h "${dst}" ] ; do
ls=`ls -ld "${dst}"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
dst="$link"
else
dst="`dirname "${dst}"`/$link"
fi
done
local bas="`basename "${dst}"`"
local dir="`dirname "${dst}"`"
if [ "$bas" != "$dir" ]; then
dst="`pathCanonical "$dir"`/$bas"
fi
echo "${dst}" | sed -e 's#//#/#g' -e 's#/\./#/#g' -e 's#/[^/]*/\.\./#/#g'
}
getActiveMQHome() 用于获取ActiveMQ的安装路径。先通过pathCanonical()获取到启动脚本的bin路径,再获取到上一级的home目录。pwd的选项-P,是为了避免所有的符号链接。
# a simple helper to get the activemq installation dir
getActiveMQHome(){
# get the real path to the binary
local REAL_BIN="`pathCanonical $0`"
local REAL_DIR="`dirname $REAL_BIN`/../"
REAL_DIR="`cd $REAL_DIR && pwd -P`"
if [ -z "$REAL_DIR" ];then
echo 'ERROR: unable to find real installtion path fo activemq, you have to define ACTIVEMQ_HOME manually in the config' >&2
exit 1
fi
echo "$REAL_DIR/"
}
3.3 几个帮助判断
配置MQ的安装目录,如果脚本启动之前的环境里没有定义ACTIVEMQ_HOME变量,则将getActiveMQHome函数获取到的路径赋值给它。
base目录,如果环境历里设置了ACTIVEMQ_BASE变量则用之,否则将ACTIVEMQ_HOME赋值给她。
# Active MQ installation dir
if [ -z "$ACTIVEMQ_HOME" ] ; then
ACTIVEMQ_HOME="`getActiveMQHome`"
fi
# Active MQ base dir
if [ -z "$ACTIVEMQ_BASE" ] ; then
ACTIVEMQ_BASE="$ACTIVEMQ_HOME"
fi
先配置用户级的classpath,脚本这里使用或者脚本外部调用。
再配置MQ的classpath,将外部lib添加到ACTIVEMQ_USER_CLASSPATH之前,合起来作为ACTIVEMQ_CLASSPATH。
# Configure user specified classpath here or externally using this variable
if [ -z "$ACTIVEMQ_USER_CLASSPATH" ] ; then
ACTIVEMQ_USER_CLASSPATH=""
fi
# ActiveMQ Classpath configuration
ACTIVEMQ_CLASSPATH="$ACTIVEMQ_BASE/../lib/:$ACTIVEMQ_USER_CLASSPATH"
MQ配置文件目录,如果变量ACTIVEMQ_CONF未配置,且老的写法ACTIVEMQ_CONFIG_DIR也未配置,则将$ACTIVEMQ_BASE/conf赋值给它。
# Active MQ configuration directory
if [ -z "$ACTIVEMQ_CONF" ] ; then
# For backwards compat with old variables we let ACTIVEMQ_CONFIG_DIR set ACTIVEMQ_CONF
if [ -z "$ACTIVEMQ_CONFIG_DIR" ] ; then
ACTIVEMQ_CONF="$ACTIVEMQ_BASE/conf"
else
ACTIVEMQ_CONF="$ACTIVEMQ_CONFIG_DIR"
fi
fi
MQ可以配置一个非root权限的用户,如果ACTIVEMQ_USER变量没有指定,则不改变,默认为当前用户。
# Configure a user with non root privileges, if no user is specified do not change user
if [ -z "$ACTIVEMQ_USER" ] ; then
ACTIVEMQ_USER=""
fi
MQ的数据目录,持久化目录,也存储了log。
如果ACTIVEMQ_DATA变量未指定,且老写法ACTIVEMQ_DATA_DIR也未指定,则将$ACTIVEMQ_BASE/data赋值给ACTIVEMQ_DATA。
# Active MQ data directory
if [ -z "$ACTIVEMQ_DATA" ] ; then
# For backwards compat with old variables we let ACTIVEMQ_DATA_DIR set ACTIVEMQ_DATA
if [ -z "$ACTIVEMQ_DATA_DIR" ] ; then
ACTIVEMQ_DATA="$ACTIVEMQ_BASE/data"
else
ACTIVEMQ_DATA="$ACTIVEMQ_DATA_DIR"
fi
fi
MQ的tmp目录
if [ -z "$ACTIVEMQ_TMP" ] ; then
ACTIVEMQ_TMP="$ACTIVEMQ_BASE/tmp"
fi
</