先写一下生产者和消费者的代码
生成者代码
/**
* Created by brady on 17/4/8.
*/
public class TestProducer {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session
.createQueue("testQueue1"));
Message message = session.createTextMessage("22223333333hello everybody!");
producer.send(message);
producer.close();
session.close();
connection.close();
}
}
消费者代码:
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("testQueue1");
consumer = session.createConsumer(destination);
// 同步的方式接收消息
while (true) {
//设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
//break;
}
}
// 异步的方式接收消息
// consumer.setMessageListener(new MessageListener(){
// @Override
// public void onMessage(Message arg0) {
// // TODO Auto-generated method stub
// try{
// if(arg0 instanceof TextMessage)
// {
// TextMessage txtMsg = (TextMessage) arg0;
// System.out.println("consumer异步 recive:"+txtMsg.getText());
// Thread.sleep(500);
// }
// }catch(Exception e)
// {
// e.printStackTrace();
// }
// }
// }); //(异步接收)
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
消费者启动时
broker会先存储消息。然后将消息发往消费者。
broker最终进入到下面的方法。messages是一个PendingMessageCursor对象。
private void doPendingCursorAdditions() throws Exception {
LinkedList<MessageContext> orderedUpdates = new LinkedList<>();
sendLock.lockInterruptibly();
try {
synchronized (indexOrderedCursorUpdates) {
MessageContext candidate = indexOrderedCursorUpdates.peek();
while (candidate != null && candidate.message.getMessageId().getFutureOrSequenceLong() != null) {
candidate = indexOrderedCursorUpdates.removeFirst();
// check for duplicate adds suppressed by the store
if (candidate.message.getMessageId().getFutureOrSequenceLong() instanceof Long && ((Long)candidate.message.getMessageId().getFutureOrSequenceLong()).compareTo(-1l) == 0) {
LOG.warn("{} messageStore indicated duplicate add attempt for {}, suppressing duplicate dispatch", this, candidate.message.getMessageId());
} else {
orderedUpdates.add(candidate);
}
candidate = indexOrderedCursorUpdates.peek();
}
}
messagesLock.writeLock().lock();
try {
for (MessageContext messageContext : orderedUpdates) {
// 注意这一行,往message后加消息
if (!messages.addMessageLast(messageContext.message)) {
// cursor suppressed a duplicate
messageContext.duplicate = true;
}
if (messageContext.onCompletion != null) {
messageContext.onCompletion.run();
}
}
} finally {
messagesLock.writeLock().unlock();
}
} finally {
sendLock.unlock();
}
for (MessageContext messageContext : orderedUpdates) {
if (!messageContext.duplicate) {
messageSent(messageContext.context, messageContext.message);
}
}
orderedUpdates.clear();
}
下面需要详细的分析下PendingMessageCursor类 实现类是StoreQueueCursor
会调用到wakeup()方法,然后会执行iterate()方法