Skip to content
Snippets Groups Projects
Commit 9bb2705c authored by Lars Erik Blümke's avatar Lars Erik Blümke
Browse files

first try on JMSReaderTest

parent 822fd3f6
No related branches found
No related tags found
No related merge requests found
package kieker.analysis.plugin.reader.jms;
import static teetime.framework.test.StageTester.test;
import java.util.Hashtable;
import java.util.List;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import org.junit.Before;
import org.junit.Test;
import kieker.common.logging.Log;
import kieker.common.logging.LogFactory;
import kieker.common.record.system.CPUUtilizationRecord;
public class JMSReaderTest {
private CPUUtilizationRecord record;
private JMSReader jmsReader;
// JMSReader constructor arguments
private final String jmsProviderUrl = "tcp://localhost:3035/";
private final String jmsDestination = "queue1";
private final String jmsFactoryLookupName = "org.exolab.jms.jndi.InitialContextFactory";
private final Log log = LogFactory.getLog(this.getClass().getName());;
// Record data
private final long timestamp = 1L;
private final String hostname = "test_host";
private final String cpuID = "cpu_1";
private final double user = 2.0;
private final double system = 3.0;
private final double wait = 4.0;
private final double nice = 5.0;
private final double irq = 6.0;
private final double totalUtilisation = 7.0;
private final double idle = 8.0;
@Before
public void initializeJMSReader() {
jmsReader = new JMSReader(jmsProviderUrl, jmsDestination, jmsFactoryLookupName, log);
record = new CPUUtilizationRecord(timestamp, hostname, cpuID, user, system, wait, nice, irq, totalUtilisation, idle);
}
@Test
public void jmsReaderShouldReadCorrectRecords() {
produceJMSMessage();
List<Object> outputList = null;
test(jmsReader).and().receive(outputList).from(jmsReader.getOutputPort()).start();
if (outputList != null) {
for (Object elem : outputList) {
System.out.println("Received" + elem);
}
}
}
public void produceJMSMessage() {
try {
final Hashtable<String, String> properties = new Hashtable<String, String>(); // NOPMD NOCS (InitialContext expects Hashtable)
properties.put(Context.INITIAL_CONTEXT_FACTORY, this.jmsFactoryLookupName);
// JMS initialization
properties.put(Context.PROVIDER_URL, this.jmsProviderUrl);
final Context context = new InitialContext(properties);
final ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
Connection connection;
connection = factory.createConnection();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination;
try {
// As a first step, try a JNDI lookup (this seems to fail with ActiveMQ sometimes)
destination = (Destination) context.lookup(this.jmsDestination);
} catch (final NameNotFoundException exc) {
// JNDI lookup failed, try manual creation (this seems to fail with ActiveMQ/HornetQ sometimes)
destination = session.createQueue(this.jmsDestination);
if (destination == null) { //
this.log.error("Failed to lookup queue '" + this.jmsDestination + "' via JNDI: " + exc.getMessage() + " AND failed to create queue");
throw exc; // will be catched below to abort the read method
}
}
// Send a monitoring record
ObjectMessage msg = session.createObjectMessage(record);
MessageProducer producer = session.createProducer(destination);
producer.send(msg);
connection.close();
} catch (NamingException ne) {
ne.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment