【11】ActiveMQ
Apache activemq
消息中间件,实现的机制很简单其实就是一个生产者 消费者 的一个机制,通过配置 xml 文件方式,更改对应配置 ,存储 使用的是 kahadb
1。java 代码使用
Sender
package ActiveMQ; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import java.sql.Time; import java.util.concurrent.TimeUnit; public class Sender { public static void main(String []arg) throws JMSException, InterruptedException { // 创建连接工厂 ConnectionFactory connectFactory = new ActiveMQConnectionFactory( "bhs","bhs", // ActiveMQConnection.DEFAULT_USER, // ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616" ); // 2 通过连接工厂 创建连接对象 Connection connection = connectFactory.createConnection(); connection.start(); //3 通过连接对象 获取session // parm1 是否支持事务 parm2 确认方式 // Session.AUTO_ACKNOWLEDGE为自动确认, // Session.CLIENT_ACKNOWLEDGE为客户端确认 // jms.Message的acknowledge方法。jms服务器才会删除消息。 // DUPS_OK_ACKNOWLEDGE允许副本的确认模式 Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); //4 .通过 session 会话对象 创建Destinations 目的对象 Queue firstMQ = session.createQueue("firstMQ"); //5. 通过 session 对象 创建消息的生产者消费者 MessageProducer producer = session.createProducer(null); //6. 设置持久化特性 // producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //7. send 消息 ,JMS 规范格式消息 for(int i=0;i<100;i++){ TextMessage msg = session.createTextMessage("自定义的消息" + i); producer.send(firstMQ,msg); //TimeUnit.SECONDS.sleep(1); } if(connection!= null){ connection.close(); } } }
Customer
package ActiveMQ; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import java.util.concurrent.TimeUnit; public class Custom { public static void main(String []arg) throws JMSException { // 创建连接工厂 ConnectionFactory connectFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616" ); // 2 通过连接工厂 创建连接对象 Connection connection = connectFactory.createConnection(); connection.start(); //3 通过连接对象 获取session // parm1 是否支持事务 parm2 确认方式 // Session.AUTO_ACKNOWLEDGE为自动确认, // Session.CLIENT_ACKNOWLEDGE为客户端确认 // jms.Message的acknowledge方法。jms服务器才会删除消息。 // DUPS_OK_ACKNOWLEDGE允许副本的确认模式 Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); //4 .通过 session 会话对象 创建Destinations 目的对象 Queue firstMQ = session.createQueue("firstMQ"); //5. 通过 session 对象 创建消息的生产者消费者 MessageConsumer consumer = session.createConsumer(firstMQ); while(true){ TextMessage msg =(TextMessage) consumer.receive(); System.out.println("消费信息" + msg.getText()); } } }
2. 这里的Sender 我采取了安全检验机制,更改了apache-activemq-5.15.8\conf\activemq.conf
添加了简单的插件
<plugins> <simpleAuthenticationPlugin> <users> <authenticationUser username="bhs" password="bhs" groups="users,admin"/> </users> </simpleAuthenticationPlugin> </plugins>
3 .还有基本配置里面的 jetty-realm.properties 这个servlet 容器,可以进行修改对应的 user pass rolename
4.配置更换持久化机制
5.独占消费
queue = new ActiveMQQueue("TEST.QUEUE?consumer.exclusive=true");
consumer = session.createConsumer(queue);
就是 一个 业务需要 有多个消息去处理,但是 需要 原子的,此时 采用独占消费的这个队列
6.订阅 更换 对应的创建队列,其实就是Topic 简单 订阅
createTopic
7.设置对应消息优先级时需要更换对应的xml
<policyEntry queue="firstMQ" prioritizedMessages="true"/>
8.JMS 过滤
直接 启动一个MessageListener 监听对象,异步处理
9.一般设计到异步处理一般都和 ExecutorService 一起用 ,处理对应数据 写一个demo
Provider
package MQ; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; import java.nio.channels.AcceptPendingException; import java.time.format.TextStyle; public class Provider { private ActiveMQConnectionFactory connectionFactory; private Connection connection; private Session session; private MessageProducer provider; private Destination destination; public Provider(String UserName ,String PsasWd ){ try { this.connectionFactory = new ActiveMQConnectionFactory( UserName, PsasWd, "tcp://localhost:61616" ); this.connection = this.connectionFactory.createConnection(); this.connection.start(); this.session = this.connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE); this.provider = this.session.createProducer(null); }catch (JMSException e){ e.printStackTrace(); } } public void connect(String topic){ try { this.destination = this.session.createTopic(topic); }catch (JMSException e){ e.printStackTrace(); } } private void send(Message msg){ try { this.provider.send(this.destination,msg,DeliveryMode.PERSISTENT,4,1000); }catch (JMSException e){ e.printStackTrace(); } } public void send(String msg){ try { TextMessage Nmsg = this.session.createTextMessage(msg); send(Nmsg); }catch (JMSException e){ e.printStackTrace(); } } public void close(){ try{ if(this.connection!=null){ this.connection.close(); } }catch (JMSException e){ e.printStackTrace(); } } public static void main(String []arg){ Provider p = new Provider("bhs","bhs"); p.connect("MyTopic"); p.send( new String ("aaaaaaaaaaaaaaaaaaaaaaaaaa")); p.close(); } }
Customer
package MQ; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; import java.nio.channels.AcceptPendingException; import java.time.format.TextStyle; public class Provider { private ActiveMQConnectionFactory connectionFactory; private Connection connection; private Session session; private MessageProducer provider; private Destination destination; public Provider(String UserName ,String PsasWd ){ try { this.connectionFactory = new ActiveMQConnectionFactory( UserName, PsasWd, "tcp://localhost:61616" ); this.connection = this.connectionFactory.createConnection(); this.connection.start(); this.session = this.connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE); this.provider = this.session.createProducer(null); }catch (JMSException e){ e.printStackTrace(); } } public void connect(String topic){ try { this.destination = this.session.createTopic(topic); }catch (JMSException e){ e.printStackTrace(); } } private void send(Message msg){ try { this.provider.send(this.destination,msg,DeliveryMode.PERSISTENT,4,1000); }catch (JMSException e){ e.printStackTrace(); } } public void send(String msg){ try { TextMessage Nmsg = this.session.createTextMessage(msg); send(Nmsg); }catch (JMSException e){ e.printStackTrace(); } } public void close(){ try{ if(this.connection!=null){ this.connection.close(); } }catch (JMSException e){ e.printStackTrace(); } } public static void main(String []arg){ Provider p = new Provider("bhs","bhs"); p.connect("MyTopic"); p.send( new String ("aaaaaaaaaaaaaaaaaaaaaaaaaa")); p.close(); } }
没有 写 对应的 过滤 以后在加=====> 未完待续
不摸着石头过河,难道要在温柔乡睡到天昏地暗。