You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by db...@apache.org on 2005/11/18 04:43:03 UTC
svn commit: r345433 - in /geronimo/gbuild/trunk/gbuild-agent/src:
main/java/org/apache/geronimo/gbuild/agent/MessagingClient.java
test/java/org/apache/geronimo/gbuild/agent/MessagingClientTest.java
Author: dblevins
Date: Thu Nov 17 19:42:56 2005
New Revision: 345433
URL: http://svn.apache.org/viewcvs?rev=345433&view=rev
Log:
Still not working. Test case proves I can't code
Added:
geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/MessagingClientTest.java
Modified:
geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/MessagingClient.java
Modified: geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/MessagingClient.java
URL: http://svn.apache.org/viewcvs/geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/MessagingClient.java?rev=345433&r1=345432&r2=345433&view=diff
==============================================================================
--- geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/MessagingClient.java (original)
+++ geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/MessagingClient.java Thu Nov 17 19:42:56 2005
@@ -31,6 +31,7 @@
import javax.jms.ObjectMessage;
import javax.jms.Message;
import javax.jms.DeliveryMode;
+import javax.jms.TextMessage;
import java.net.SocketException;
import java.io.Serializable;
import java.util.HashMap;
@@ -50,7 +51,11 @@
private Connection connection;
private boolean failed;
private int maxAttempts = 10;
+ private JMSException exception;
+ public MessagingClient(String transportUrl, Logger logger) throws JMSException {
+ this(transportUrl, logger, new NullListener());
+ }
public MessagingClient(String transportUrl, Logger logger, ExceptionListener listener) throws JMSException {
this.transportUrl = transportUrl;
this.logger = logger;
@@ -60,6 +65,11 @@
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
+ public static class NullListener implements ExceptionListener {
+ public void onException(JMSException jmsException) {
+ }
+ }
+
public Logger getLogger() {
return logger;
}
@@ -122,8 +132,11 @@
}
- private Destination getDestination(String subject) {
+ private Destination getDestination(String subject) throws JMSException {
synchronized (destinations) {
+ if (failed) {
+ throw exception;
+ }
return (Destination) destinations.get(subject);
}
}
@@ -138,8 +151,8 @@
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(transportUrl);
Connection connection = connectionFactory.createConnection();
- connection.start();
connection.setExceptionListener(this);
+ connection.start();
return connection;
} catch (JMSException e) {
if (tries >= maxAttempts){
@@ -156,17 +169,17 @@
public void onException(JMSException jmsException) {
getLogger().error(jmsException.getMessage());
- Throwable cause = jmsException.getCause();
- if (!failed && cause instanceof SocketException) {
+ if (failed) {
+ listener.onException(jmsException);
+ } else {
try {
reset();
} catch (JMSException e) {
getLogger().error("Unable to restablish a connection to "+transportUrl);
failed = true;
+ exception = jmsException;
listener.onException(jmsException);
}
- } else {
- listener.onException(jmsException);
}
}
}
Added: geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/MessagingClientTest.java
URL: http://svn.apache.org/viewcvs/geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/MessagingClientTest.java?rev=345433&view=auto
==============================================================================
--- geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/MessagingClientTest.java (added)
+++ geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/MessagingClientTest.java Thu Nov 17 19:42:56 2005
@@ -0,0 +1,134 @@
+package org.apache.geronimo.gbuild.agent;
+/**
+ * @version $Rev$ $Date$
+ */
+
+import junit.framework.TestCase;
+import org.activemq.broker.impl.BrokerContainerImpl;
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.BasicConfigurator;
+
+import javax.jms.JMSException;
+import javax.jms.ExceptionListener;
+import javax.jms.Message;
+import javax.jms.ObjectMessage;
+
+public class MessagingClientTest extends TestCase {
+ private TestLogger logger;
+
+ public void testSend() throws Exception {
+
+ logger = new TestLogger("hive");
+ String hiveLocation = "tcp://localhost:44668";
+
+ BrokerContainerImpl behive = new BrokerContainerImpl();
+ behive.addConnector(hiveLocation);
+
+ logger.debug("STARTING...");
+ behive.start();
+ logger.debug("STARED");
+
+ BusyBee[] bees = new BusyBee[3];
+ for (int i = 0; i < bees.length; i++) {
+ bees[i] = new BusyBee("bee"+i, hiveLocation);
+ }
+
+ work(10000);
+
+ logger.debug("STOPPING...");
+ behive.stop();
+ logger.debug("STOPPED");
+
+ rest(1000);
+
+ logger.debug("STARTING...");
+ behive.start();
+ logger.debug("STARED");
+
+ work(10000);
+
+ logger.debug("STOPPING...");
+ behive.stop();
+ logger.debug("STOPPED");
+
+ rest(1000);
+
+ logger.debug("STARTING...");
+ behive.start();
+ logger.debug("STARED");
+
+ work(10000);
+
+ for (int i = 0; i < bees.length; i++) {
+ BusyBee bee = bees[i];
+ assertTrue("alive", bee.isRunning());
+ assertNotNull("fatality", bee.fatality);
+ bee.die();
+ }
+
+ logger.debug("STOPPING...");
+ behive.stop();
+ logger.debug("STOPPED");
+ logger.debug("DONE");
+ }
+
+
+ private void work(int i) throws InterruptedException {
+ Thread.sleep(i);
+ }
+
+ private void rest(int i) throws InterruptedException {
+ Thread.sleep(i);
+ }
+
+ public static class BusyBee implements Runnable, ExceptionListener {
+ private final MessagingClient client;
+ private boolean run;
+ public Exception fatality;
+ private TestLogger logger;
+ private String name;
+
+ public BusyBee(String name, String url) throws JMSException {
+ this.logger = new TestLogger(name);
+ this.client = new MessagingClient(url, logger, this);
+ this.name = name;
+ Thread thread = new Thread(this);
+ thread.setDaemon(true);
+ thread.start();
+ }
+
+ public void run() {
+ run = true;
+
+ try {
+ String subject = "flowers";
+ client.addQueue(subject);
+
+ while (run) {
+ client.send(subject, name + " collecting pollen");
+ ObjectMessage message = (ObjectMessage) client.receive(subject, 30000);
+ logger.debug("Buzzzz ... "+message.getObject());
+ }
+ } catch (JMSException e) {
+ logger.error("Hive Destroyed!!! ("+e.getMessage()+")");
+ fatality = e;
+ run = false;
+ }
+ }
+
+ public synchronized boolean isRunning() {
+ return run;
+ }
+
+ public synchronized void die(){
+ run = false;
+ }
+
+ public void onException(JMSException jmsException) {
+ logger.error("Can't Find Hive!!! ("+jmsException.getMessage()+")");
+ fatality = jmsException;
+ run = false;
+ }
+ }
+}
\ No newline at end of file