[jboss-svn-commits] JBL Code SVN: r22939 - labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Sep 20 09:29:11 EDT 2008
Author: tfennelly
Date: 2008-09-20 09:29:11 -0400 (Sat, 20 Sep 2008)
New Revision: 22939
Modified:
labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceInvoker.java
labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceRouter.java
Log:
service invoker
Modified: labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceInvoker.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceInvoker.java 2008-09-20 10:50:08 UTC (rev 22938)
+++ labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceInvoker.java 2008-09-20 13:29:11 UTC (rev 22939)
@@ -19,9 +19,24 @@
*/
package org.jboss.esb;
+import org.jboss.esb.context.AddressingContext;
+import org.jboss.esb.context.DeploymentContext;
+import org.jboss.esb.context.InvocationContext;
+import org.jboss.esb.deploy.DeploymentRuntime;
+import org.jboss.esb.deploy.DeploymentUtil;
+import org.jboss.esb.federate.DeploymentCoordinator;
+import org.jboss.esb.federate.DeploymentMonitor;
+import org.jboss.esb.federate.bus.BusInboundRouter;
+import org.jboss.esb.federate.bus.BusMessage;
+import org.jboss.esb.federate.bus.BusRoutingContext;
import org.jboss.esb.message.Message;
-import org.jboss.esb.service.ServiceName;
+import org.jboss.esb.routing.RoutingException;
+import org.jboss.esb.util.AssertArgument;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
/**
* Service Invoker.
*
@@ -33,36 +48,138 @@
public class ServiceInvoker
{
/**
- * Get the ServiceInvoker instance for the deployment.
- * @return The ServiceInvoker instance.
+ * Deployment context.
*/
- public static ServiceInvoker getServiceInvoker()
+ private DeploymentContext deploymentContext;
+ /**
+ * Deployment runtime.
+ */
+ private DeploymentRuntime deploymentRuntime;
+
+ /**
+ * Public constructor.
+ * @param deploymentContext The deployment context associated with this invoker instance.
+ */
+ public ServiceInvoker(final DeploymentContext deploymentContext)
{
- return null;
+ AssertArgument.isNotNull(deploymentContext, "deploymentContext");
+ this.deploymentContext = deploymentContext;
+ deploymentRuntime = DeploymentRuntime.getRuntime(deploymentContext);
}
/**
* Send the supplied message to the specified Service.
- * <p/>
- * Supports an asynchronous replyTo.
*
* @param message The message to be sent.
- * @param to The Service to which the message is to be sent.
- * @param replyTo The Service to which the reply should be sent.
+ * @param addressingContext
+ * @throws org.jboss.esb.routing.RoutingException
*/
- public void send(final Message message, final ServiceName to, final ServiceName replyTo)
+ public void send(Message message, AddressingContext addressingContext) throws RoutingException
{
+ if(addressingContext.getTo() == null)
+ {
+ throw new RoutingException("Invalid AddressingContext configuration. The 'to' service name must be specified.");
+ }
+ // The "to" service name should not be this service...
+ if(addressingContext.getTo().equals(addressingContext.getFrom()))
+ {
+ throw new RoutingException("Invalid AddressingContext configuration. The 'to' and 'from' service names (" + addressingContext.getTo() + ") cannot be the same.");
+ }
+
+ // Work out whether or not the target service is local,
+ // or on another deployment...
+ boolean isLocalService = false;
+ if(DeploymentUtil.getService(addressingContext.getTo(), deploymentRuntime) != null)
+ {
+ isLocalService = true;
+ }
+
+ if(isLocalService)
+ {
+ routeLocal(message, addressingContext);
+ }
+ else
+ {
+ routeNonLocal(message, addressingContext);
+ }
}
+
/**
- * Send the supplied message to the specified Service.
- *
- * @param message The message to be sent.
- * @param to The Service to which the message is to be sent.
+ * Route the message to a local Service.
+ * @param message The message.
+ * @param invokeAddressingContext The {@link AddressingContext} for the invocation.
+ * @throws RoutingException Exception during invocation.
*/
- public void sendOneWay(final Message message, final ServiceName to)
+ private void routeLocal(final Message message, final AddressingContext invokeAddressingContext) throws RoutingException
{
+ InvocationContext thisInvocationContext = InvocationContext.getContext();
+ AddressingContext thisAddressingContext = AddressingContext.getContext();
+ // Set the Addressing context for the new Service Invocation...
+ AddressingContext.setContext(invokeAddressingContext);
+ try
+ {
+ try
+ {
+ BusRoutingContext routingContext = BusRoutingContext.getContext(deploymentContext);
+ BusInboundRouter inRouter = routingContext.getBusInRouters().get(invokeAddressingContext.getTo());
+
+ // Going to use the same InvocationContext...
+ inRouter.dispatch(message, thisInvocationContext);
+ }
+ finally
+ {
+ // Make sure the invocation context is still set on the
+ // local thread...
+ InvocationContext.setContext(thisInvocationContext);
+ }
+ }
+ finally
+ {
+ // Reset the addressing context...
+ AddressingContext.setContext(thisAddressingContext);
+ }
}
+
+ /**
+ * Route the message to a non-local Service.
+ * @param message The message.
+ * @param invokeAddressingContext The {@link AddressingContext} for the invocation.
+ * @throws RoutingException Exception during invocation.
+ */
+ private void routeNonLocal(final Message message, final AddressingContext invokeAddressingContext) throws RoutingException
+ {
+ InvocationContext thisInvocationContext = InvocationContext.getContext();
+ List<DeploymentCoordinator.BusDeployment> busDeployments = deploymentRuntime.getDeploymentCoordinator().getBusDeployments();
+
+ // Iterate over all the Bus deployments looking for a deployment that
+ // is online and is hosting the target service...
+ for (DeploymentCoordinator.BusDeployment busDeployment : busDeployments)
+ {
+ Set<Map.Entry<String, DeploymentMonitor>> monitors = busDeployment.getDeploymentMonitors().entrySet();
+ for (Map.Entry<String, DeploymentMonitor> entry : monitors)
+ {
+ String deploymentId = entry.getKey();
+ DeploymentMonitor monitor = entry.getValue();
+
+ // Is the deployment online and does it host the target service...
+ if(monitor.isOnline() && monitor.hasService(invokeAddressingContext.getTo()))
+ {
+ BusMessage busMessage = new BusMessage();
+
+ busMessage.setMessage(message);
+ busMessage.setInvocationParameters(thisInvocationContext.getInvocationParameters());
+ busMessage.setAddressingContext(invokeAddressingContext);
+
+ monitor.getBus().send(busMessage, deploymentId);
+
+ return;
+ }
+ }
+ }
+
+ throw new RoutingException("Failed to route message from service '" + invokeAddressingContext.getFrom() + "' to service '" + invokeAddressingContext.getTo() + "'. Target service is not available.");
+ }
}
Modified: labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceRouter.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceRouter.java 2008-09-20 10:50:08 UTC (rev 22938)
+++ labs/jbossesb/workspace/skeagh/routing/esb/src/main/java/org/jboss/esb/ServiceRouter.java 2008-09-20 13:29:11 UTC (rev 22939)
@@ -23,24 +23,12 @@
import org.jboss.esb.annotations.Property;
import org.jboss.esb.context.AddressingContext;
import org.jboss.esb.context.DeploymentContext;
-import org.jboss.esb.context.InvocationContext;
import org.jboss.esb.deploy.DeploymentException;
-import org.jboss.esb.deploy.DeploymentRuntime;
-import org.jboss.esb.deploy.DeploymentUtil;
-import org.jboss.esb.federate.DeploymentCoordinator;
-import org.jboss.esb.federate.DeploymentMonitor;
-import org.jboss.esb.federate.bus.BusInboundRouter;
-import org.jboss.esb.federate.bus.BusMessage;
-import org.jboss.esb.federate.bus.BusRoutingContext;
import org.jboss.esb.message.Message;
import org.jboss.esb.routing.OutboundRouter;
import org.jboss.esb.routing.RoutingException;
import org.jboss.esb.service.ServiceName;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
/**
* Service Message {@link OutboundRouter}.
* <p/>
@@ -55,10 +43,6 @@
*/
private ServiceName serviceName;
/**
- * Is the "to" Service local i.e. within the local deployment.
- */
- private boolean isLocalService = false;
- /**
* Deployment context.
*/
private DeploymentContext deploymentContext;
@@ -105,10 +89,9 @@
*/
private ServiceName replyToServiceName;
/**
- * Local reference to the deployment runtime within which this
- * router is executing.
+ * Service Invoker.
*/
- private DeploymentRuntime deploymentRuntime;
+ private ServiceInvoker invoker;
/**
* Initialize the router.
@@ -116,15 +99,9 @@
@Initialize
public void initialize() throws DeploymentException
{
- deploymentRuntime = DeploymentRuntime.getRuntime(deploymentContext);
+ invoker = new ServiceInvoker(deploymentContext);
toServiceName = new ServiceName(toCategory, toService);
- // The "to" service name should not be this service...
- if(toServiceName.equals(serviceName))
- {
- throw new DeploymentException("Invalid ServiceRouter configuration on deployment '" + deploymentRuntime.getDeploymentName() + "'. The 'to' service name (" + serviceName + ") cannot be the name of the service itself.");
- }
-
if(faultToCategory != null && faultToService != null)
{
faultToServiceName = new ServiceName(faultToCategory, faultToService);
@@ -133,13 +110,6 @@
{
replyToServiceName = new ServiceName(replyToCategory, replyToService);
}
-
- // Work out whether or not the target service is local,
- // or on another deployment...
- if(DeploymentUtil.getService(toServiceName, deploymentRuntime) != null)
- {
- isLocalService = true;
- }
}
/**
@@ -157,90 +127,6 @@
addressingContext.setReplyTo(replyToServiceName);
addressingContext.setFaultTo(faultToServiceName);
- if(isLocalService)
- {
- routeLocal(message, addressingContext);
- }
- else
- {
- routeNonLocal(message, addressingContext);
- }
+ invoker.send(message, addressingContext);
}
-
- /**
- * Route the message to a local Service.
- * @param message The message.
- * @param invokeAddressingContext The {@link AddressingContext} for the invocation.
- * @throws RoutingException Exception during invocation.
- */
- private void routeLocal(final Message message, final AddressingContext invokeAddressingContext) throws RoutingException
- {
- InvocationContext thisInvocationContext = InvocationContext.getContext();
- AddressingContext thisAddressingContext = AddressingContext.getContext();
-
- // Set the Addressing context for the new Service Invocation...
- AddressingContext.setContext(invokeAddressingContext);
- try
- {
- try
- {
- BusRoutingContext routingContext = BusRoutingContext.getContext(deploymentContext);
- BusInboundRouter inRouter = routingContext.getBusInRouters().get(toServiceName);
-
- // Going to use the same InvocationContext...
- inRouter.dispatch(message, thisInvocationContext);
- }
- finally
- {
- // Make sure the invocation context is still set on the
- // local thread...
- InvocationContext.setContext(thisInvocationContext);
- }
- }
- finally
- {
- // Reset the addressing context...
- AddressingContext.setContext(thisAddressingContext);
- }
- }
-
- /**
- * Route the message to a non-local Service.
- * @param message The message.
- * @param invokeAddressingContext The {@link AddressingContext} for the invocation.
- * @throws RoutingException Exception during invocation.
- */
- private void routeNonLocal(final Message message, final AddressingContext invokeAddressingContext) throws RoutingException
- {
- InvocationContext thisInvocationContext = InvocationContext.getContext();
- List<DeploymentCoordinator.BusDeployment> busDeployments = deploymentRuntime.getDeploymentCoordinator().getBusDeployments();
-
- // Iterate over all the Bus deployments looking for a deployment that
- // is online and is hosting the target service...
- for (DeploymentCoordinator.BusDeployment busDeployment : busDeployments)
- {
- Set<Map.Entry<String, DeploymentMonitor>> monitors = busDeployment.getDeploymentMonitors().entrySet();
- for (Map.Entry<String, DeploymentMonitor> entry : monitors)
- {
- String deploymentId = entry.getKey();
- DeploymentMonitor monitor = entry.getValue();
-
- // Is the deployment online and does it host the target service...
- if(monitor.isOnline() && monitor.hasService(toServiceName))
- {
- BusMessage busMessage = new BusMessage();
-
- busMessage.setMessage(message);
- busMessage.setInvocationParameters(thisInvocationContext.getInvocationParameters());
- busMessage.setAddressingContext(invokeAddressingContext);
-
- monitor.getBus().send(busMessage, deploymentId);
-
- return;
- }
- }
- }
-
- throw new RoutingException("Failed to route message from service '" + serviceName + "' to service '" + toServiceName + "'. Target service is not available.");
- }
}
More information about the jboss-svn-commits
mailing list