Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Dependency Injection

    How do I Inject an EJB Stateless Session Bean into a DMI DataSource?

    My current architecture makes use of standard Stateless Session Beans to hold a fair amount of business logic, so using the SmartGWT JPA2DataSource will circumvent this. What I would like to do is call the standard J2EE components from the DMI.

    Is there are example, or document that shows this?

    Thanks

    #2
    Yes there is extensive documentation and samples. Start with the QuickStart Guide, Server Framework chapter.

    Comment


      #3
      Documentation complete

      Hi Isomorphic,

      This seams to be a standard response from you. You assume everyone has not yet read the Quick start guide? I have read the documentation, I have also combed through the examples, I have included an example below of how to lookup a stateless session bean from the initial context, this works from a DMI DataSource fetch call.

      @EJB(name = "SLBean", beanInterface = SLBean.class)
      public class DataSourceDMI {
      SLBean myStatelessBean;

      //My fetch method for the data call
      public DSResponse fetch(DSRequest dsRequest) throws Exception {
      // Look up the stateless session bean
      InitialContext ic = new InitialContext();
      myStatelessBean = (SLBean) ic.lookup("java:comp/env/SLBean");

      long startRow = dsRequest.getStartRow();
      long endRow = dsRequest.getEndRow();
      long totalRows = myStatelessBean.getData().size();

      DSResponse dsResponse = new DSResponse();
      dsResponse.setData(myStatelessBean.getData());
      dsResponse.setTotalRows(totalRows);
      dsResponse.setStartRow(startRow);
      dsResponse.setEndRow(Math.min(endRow, totalRows));

      return dsResponse;
      }
      }

      My question to you was how do you INJECT the stateless session bean into the datasource? The code above is typically used to lookup a Statefull Session Bean, since I am using a Stateless Session Bean, the normal approach would be to use the @EJB annotation, such as:

      public class DataSourceDMI {
      @EJB SLBean myStatelessBean;

      //My fetch method for the data call
      public DSResponse fetch(DSRequest dsRequest) throws Exception {
      long startRow = dsRequest.getStartRow();
      long endRow = dsRequest.getEndRow();
      long totalRows = myStatelessBean.getData().size();

      DSResponse dsResponse = new DSResponse();
      dsResponse.setData(myStatelessBean.getData());
      dsResponse.setTotalRows(totalRows);
      dsResponse.setStartRow(startRow);
      dsResponse.setEndRow(Math.min(endRow, totalRows));

      return dsResponse;
      }
      }

      This does not work since the EJB resource is never injected into the DataSourceDMI class. If there is a different approach that you had in mind when it comes to INJECTION please let me know.

      Thanks

      Comment


        #4
        Originally posted by phickey View Post
        You assume everyone has not yet read the Quick start guide?
        Stick around long enough, and you'll see why that is. :-)

        The attribute lookupStyle controls how the server framework obtains an instance of DMIHandler. In the sample above, lookupStyle is not specified, so an instance of DMIHandler is created exactly as though the code new DMIHandler() were executed.

        Other options for lookupStyle allow you to:

        * target objects in the current servlet request or servlet session

        * obtain objects via a factory pattern

        * obtain objects via the Spring framework, including the ability to use Spring’s “dependency injection” to set up the target object
        AFAIK, there is no way to have your DMI managed by the container (which is I'm pretty sure what you'd need to get @EJB to work without Spring), so... you were looking for clarification on how to use Spring?

        Comment


          #5
          Best practices

          Hi,

          Thanks, yeah it seams that access to the container is limited. So from our perspective, my company is interested in the slick and very professional widget base provided by SmartGWT. In our opinion we have not yet found a more attractive set of predefined UI controls.

          Having said that it is also important that we stay close to the standard J2EE technology for the business logic and persistence layer, because it is proven and supplies us with the required clustering, load balancing, etc, not to mention our already invested application and human capital that is built largely on standard Java technologies.

          I have also realized that the SmartGWT implementation has a lot of efficiencies built into the server framework, like data compression between the backend and the fronted, and the tight integration between the SmartGWT server and UI.

          So what we are looking to do is build the link between the SmartGWT server and our chosen business logic and persistence layer when needed (Obviously we will use the standards like SQL/JPA datasources for reporting and etc when no business logic is required). The documentation that I have read gives us a few options, use a RESTful datasource, create a custom data source, or use DMI.

          Since the server is involved with all three options (And thus IDCall servlet) I was curious to see if there was a way to "Inject" the required EJB references into the servlet and then pass those to a custom data source or DMI method.

          If anyone has done a similar integration I would love to hear about their experiences, a best practices document on these integrations would be of very high value, or a component diagram showing how the SmartGWT server framework slots into the big picture, what should be changed to ensure that the SmartGWT UI is still compatible with the datasources, how do we ensure that all of the features in the SmartGWT server are cantered for, like paging, selection criteria, exception handling, and error messaging? (I know that there are a few examples in the show case, but they are admittedly not complete and only examples of how one might go about doing it).

          Thanks
          Last edited by phickey; 4 Feb 2013, 07:48.

          Comment


            #6
            This is def in the quick start guide.

            Look at ServerObject and lookup style 'bean'

            Having worked with the tech for a couple of years now - I actually think the backend/server aspects of SmartGWT is its best feature.

            Comment


              #7
              Not in guide

              Hi Amcculley,

              I had another look at the quick start guide. There is no reference to SeverObject with a lookup style of type "bean". Also I forward navigated from the quick start guide to the ServerObject javdocs. I have cut and paste the text here so that you can read it:

              =================================================
              lookupStyle

              public java.lang.String lookupStyle

              Specifies the mechanism for locating the class instance on which to invoke the method. Valid values are as follows:

              "spring": For use with the Spring framework. bean contains the name of the bean to invoke. Which application context is used can be configured via web.xml (see the example web.xml in the SDK).

              "new": A new instance of the class specified by className will be created and the DMI method will be invoked on that instance (unless the specified method is static, in which case no instance is created, but the class specified by className is still used).

              "factory": A custom factory provides the class instance on which the DMI method is to be invoked. In this case, className specifies the className of the factory that will provide the instance on which the DMI method is to be invoked. The class specified by className must provide exactly one method named create that must return the class instance on which you wish the DMI method to be invoked. Like the DMI methods, the create method can request a standard set of values as arguments. See DMI for a list of available values.

              "attribute": The instance on which the DMI method is to be invoked is looked up in the scope defined by attributeScope via the attribute name specified in attributeName.

              Default value is "new"
              =================================================

              I have also included the link:

              http://www.smartclient.com/smartgwtee/javadoc/com/smartgwt/client/docs/serverds/ServerObject.html#lookupStyle

              There is no such reference to "bean". Are you perhaps using a custom built DataSource or a different document to get this information from?

              Thanks

              Comment


                #8
                No help?

                Hi guys has this thread dried up?

                Comment


                  #9
                  The advice from bbryun & amcculley is completely correct, but amcculley inadvertently used the term "bean", whereas from the documentation you posted, "spring" is clearly what he meant.
                  Last edited by Isomorphic; 8 Feb 2013, 09:51.

                  Comment

                  Working...
                  X