我试图估计发送者和接收者之间的ActiveMQ通信的转移持续时间。我不知道我怎么能同时得到我的变量sending_time和reception_time,所以我可以估计transfert_duration = reception_time - 发送时间。
发信人:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageProducer producer = null;
public long sending_time;
public Sender() {
}
public void sendMessage() {
try {
factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("QUEUE");
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
message.setText("Hello");
sending_time = System.currentTimeMillis();
producer.send(message);
System.out.println("Sent : " + message.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Sender sender = new Sender();
sender.sendMessage();
}
}

接收器:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Receiver {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageConsumer consumer = null;
public long reception_time;
public Receiver() {
}
public void receiveMessage() {
try {
factory = new ActiveMQConnectionFactory( "tcp://localhost:61616");
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("QUEUE");
consumer = session.createConsumer(destination);
System.out.println("Waiting for messages...");
while( true ){
Message message = consumer.receive();
reception_time = System.currentTimeMillis();
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
System.out.println("Received : " + text.getText());
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Receiver receiver = new Receiver();
receiver.receiveMessage();
}
}