Command Pattern and Local EJB


General J2EE: Command Pattern and Local EJB

  1. Command Pattern and Local EJB (2 messages)


    I'm thinking (yes!) about using command pattern in my system (not huge but not small - mainly EJBs).
    And I have one question..
    Command is executed localy on AppServer Right?
    So why should I use REMOTE INTERFACE instead of Local ones?
    In my opiniony it could be easier and faster to access to all sessionbeans by using Local interfaces.
    Am I Right? Or maybe there is some... hack?

    What do you think about this?


    Threaded Messages (2)

  2. Command Pattern and Local EJB[ Go to top ]

    Actually you could take this even further and have the command objects as ordinary java classes (i.e. not as EJB's at all). This could make sense if they did not require the services of the EJB container.
  3. Command Pattern and Local EJB[ Go to top ]

    The typical use of the command pattern for EJB's is to make your command objects normal serializable Java objects. The command objects can be initialized by the client, passed to the EJB server, executed inside a transaction environment, then passed back to the client with the results.

    // Custom commands will be subclasses of this interface
    public interface Command extends Serializable {
      void execute() throws CommandException;

    // A session bean to execute the command:
    public class CommandExcecutorBean implements SessionBean {

      public Command processCommand(Command command) throws CommandException {
        try {
        } catch(CommandException ex) {

      // Other code ...

    // In your client, use a subclass of Command:
    MyCommand command = new MyCommand();
    // Initialize the command with data ...
    CommandExcecutor executor = // lookup CommandExcecutor EJB ...
    MyCommand result = (MyCommand) executor.processCommand(command);
    // Retrieve results for command ...

    The advantage of this pattern is that your commands are ordinary Java objects and do not require elaborate configuration the way that an EJB does. This is also a disadvantage: your commands cannot be configured to have specialized security constaints or transaction attributes the way that your EJBs can.