4 Test Verification 4.1 Java Client Connections
Using ACTIVEMQ in a Java project
Importing the packages required for ACTIVEMQ in Java Engineering
The following packages are required:
Activemq-core.jar
Activeio-core.jar
Kahadb.jar (ifyou wish to use persistence if this jar package is needed for persistence)
Slf4j-api.jar
ee Jars
geronimo-spec-jms.jar
Geronimo-spec-jta.jar
Geronimo-spec-j2ee-management.jar
You can also use the default Activemq-all.jar to download the address:
Http://mvnrepository.com/artifact/org.apache.activemq/activemq-all
4.2 Producer Testing
public class Producer {private static final String Broker_url = "tcp://localhost:61616";
private static final Boolean non_transacted = false;
private static final int num_messages_to_send = 100;
Private static final long DELAY = 100;
public static void Main (string[] args) {String url = broker_url;
if (Args.length > 0) {url = Args[0].trim ();
} activemqconnectionfactoryconnectionfactory = new Activemqconnectionfactory ("admin", "admin", url);
Connection Connection = null;
try {connection = connectionfactory.createconnection ();
Connection.start ();
Session session =connection.createsession (non_transacted, Session.auto_acknowledge);
Destination Destination =session.createqueue ("Test-queue");
MessageProducer producer =session.createproducer (destination); for (int i = 0; i < num_messages_to_send; i++) {TExtmessage message =session.createtextmessage ("message#" + i);
SYSTEM.OUT.PRINTLN ("Sending message #" + i);
Producer.send (message);
Thread.Sleep (DELAY);
} producer.close ();
Session.close ();
} catch (Exception e) {System.out.println ("caught exception!");
} finally {if (connection! = null) {try {connection.close ();
} catch (JMSException e) {System.out.println ("Could not close an open connection ..."); }
}
}
}
}
4.3 consumer testing
public class Consumer {private static final String Broker_url = "tcp://localhost:61616";
private static final Boolean non_transacted = false;
Private static final long TIMEOUT = 20000;
public static void Main (string[] args) {String url = broker_url;
if (Args.length > 0) {url = Args[0].trim ();
} System.out.println ("\nwaiting to receive messages ... would timeout after" + timeout/1000 + "s");
Activemqconnectionfactoryconnectionfactory = new Activemqconnectionfactory ("admin", "admin", url);
Connection Connection = null;
try {connection =connectionfactory.createconnection ();
Connection.start ();
Session session =connection.createsession (non_transacted, Session.auto_acknowledge);
Destination Destination =session.createqueue ("Test-queue");
Messageconsumer consumer =session.createconsumer (destination);
int i = 0; while (true) {message message =consumer.receive (TIMEOUT); if (message = null) {if (message instanceof textmessage) {String Text = (Te
xtmessage) message). GetText (); System.out.println ("Got" + i++ + ".
Message: "+ text");
}} else {break;
}} consumer.close ();
Session.close ();
} catch (Exception e) {System.out.println ("caught exception!");
} finally {if (connection! = null) {try {connection.close ();
} catch (JMSException e) {System.out.println ("Could not close an open connection ..."); }
}
}
}
}
4.4 Data View
To log into the MQ View interface:
http://192.168.xxx.xxx:8161/
View ACTIVEMQ =>localhost=queue After login to view data sent by the producer
The following figure:
When the consumer finishes, the queue data is empty when viewing the interface;
The following figure: