需要jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
application.yml中配置
spring:
activemq:
user: admin
password: admin
broker-url: tcp://ip:61616
in-memory: true
pool:
expiry-timeout: 10000 //过期时间
idle-timeout: 30000 //链接时间
max-connections: 50 //最大链接数
enabled: true //连接池是否可用
@Component
public class ActiveMqUtill{
private static Logger logger = LoggerFactory.getLogger(ActiveMqUtill.class);
@Autowired
private JmsMessagingTemplate jsmMessagingTemplate;
@Autowired
private static JmsMessagingTemplate jsmMessagingTemplate1;
@PostConstruct//静态方法中应用实例bean,这个是java中在servlet实例化是创建
public void init() {
jsmMessagingTemplate1 = jsmMessagingTemplate;
}
public <T> boolean sendMess(T contents,Destination destination){
try {
jsmMessagingTemplate1.convertAndSend(destination, contents);
logger.info("发送成功");
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
activemq下载之后,解压,进入到bin文件夹中,
activemq install 进行服务安装
activemq start 进行服务启动,
activemq stop 服务停止
activemq uninstall 服务卸载
一般如果不改密码的话,http://localhost:8161/admin/send.jsp 账户密码是admin.
java 进行连接的时候,可以直接使用默认密码
package myq.myq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* Hello world!
*
*/
public class sender
{
static int SEND_NUMBER=10;
public static void main( String[] args ) throws Exception
{
ConnectionFactory cFactory;
Connection connection;
Destination destination; // MessageProducer:消息发送者
MessageProducer producer; // TextMessage message;
Session session;
cFactory=new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
connection=cFactory.createConnection();
connection.start();
session= connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("FirstQueu1e");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息,此处写死,项目就是参数,或者方法获取
sendMessage(session, producer);
session.commit();
session.close();
connection.close();
}
public static void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session.createTextMessage("ActiveMq 发送的消息"
+ i);
// 发送消息到目的地方
System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
producer.send(message);
}
}
}
如果此时密码填写错误,或者密码不填写,同样可以连接mq,我猜想是java mq机制问题,如果密码错误,或者不贴他就zhijie
使用默认密码进行连接。
考虑到安全性,我们一般在使用的时候,就需要把activemq的默认密码进行修改,以保证数据安全,
activemq.xml文件中systemUsage标签前面增加如下,增加访问用户
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="admin1" password="admin1" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
通过如下方式修改默认admin密码
activemq是通过jitty发布的 ,在jitty.xml 中 <bean id="adminSecurityConstraint" class="org.eclipse.jetty.util.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="admin" />
<!-- set authenticate=false to disable login -->
<property name="authenticate" value="true" />
</bean>
把authenticate 的value改成ture
在进行修改
jetty-realm.properties文件
admin: admin1, admin
user: user, user
格式是:用户名 :密码 ,权限