Announcement

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

    #31
    Need for clean publish/subscribe API (AKA Observer pattern)

    Originally posted by sjivan View Post
    I would recommend using native GWT's EventBus API's instead of Tibco EventBus unless you need to do cross frame communication via an event bus. Tibco EventBus was useful prior to the introduction of EventBus in core GWT.

    See this link on how to use GWT's EventBus.
    This may not be the right discussion thread in regards to a publish/subscribe API that works in the GWT environment.

    My comments below will be code design oriented and they are not a bug report. Please forgive me if this is not the right place to post such a comment.

    My motivation for using a Java reflection API that works in the GWT environment was driven by the lack of simple and available publish/subscribe APIs. I mainly needed access to annotations and the invoke capability.

    IMHO, A publish/subscribe API is crucial to writing clean loosely coupled UI code.

    Through Google searches is seems the publish/subscribe API most recommended is GWT's EventBus API.

    I looked at this and wrote some sample code. I felt this was not a great solution because:
    • To implement even a single event takes a about 15 lines of code.
    • The 15 lines of code are very complex, hard to understand, and hard to read. It uses Java language constructs that probably are not familiar to many Java programmers (me included!).
    • The use of this API doesn’t produce readable code. If I would like to create and use many events then my code’s readability will become polluted with this code. One of the great things about SmartGWT is that if it is used right it produces very readable code.


    Consequently I produced a very simple publish/subscribe API using GWT ENT .

    Here is a subscriber code example.

    Code:
    public class SomeSubscriber {
    	
    	private int eventCount = 0;
    	String whatIsSomething = "No event yet";
    	
    	public SomeSubscriber(){
    		EventHub.subscribe(this);
    	}
    
    	@Listener
    	public void handler1(Event1 event1){
    		whatIsSomething = event1.getSomething();
    		eventCount++;
    	}
    }
    Here is a publish code example:

    Code:
    Event1 event1 = new Event1();
    Event1.setSomething(“this is something”);
    EventHub.publish(event1);
    This code works.* I'm happy to share this library with anyone interested.

    (*I was able to find a work-around for the compile failure I was experiencing with GWT-ENT)

    Comment


      #32
      If you have an API that works for you, then there's no need to look any further.

      Most of the code required when adding a new event using the GWT API's is pretty boilerplate and once you have a closer look you'll recognize that all events pretty much look similar besides what parameters they hold. Also GWT EventBus has a mechanism to unsubscribe by calling removeHandler() on the HandlerRegistration object that is returned when EventBus#addHandler(..) is called.

      Comment


        #33
        Hi DBScott,

        *I was able to find a work-around for the compile failure I was experiencing with GWT-ENT

        Can you please help to post your workaround for the compiler failure

        Comment


          #34
          GWTENT work around

          The problem was with ENT's annotation class. Here is the code I had to implement on the annotation class:

          Code:
          	private String annotationToString(Annotation annotation) {
          
          		/**
          		 * IMPORTANT: The expression below is needed because apparently the code
          		 * generation of the GWTENT library prevents the use of String methods
          		 * directly on the annotation name. This is a GWTENT bug.
          		 * 
          		 * By concatenating the annotation name with a blank string a new string
          		 * is created. This new string allows String methods to be performed.
          		 */
          
          		return "" + annotation.toString();
          	}
          I should point out that I abandoned using GWTENT for publish/subscribe. Instead I implemented a home-grown way of doing this with an enum.

          I do use GWTENT for marshaling data between Java beans and SmartGWT records and criteria.
          Last edited by dbscott525; 16 Sep 2014, 13:38.

          Comment


            #35
            Thanks for posting that, it may help someone else.

            Just in case - there's no way we could modify SmartGWT to avoid this problem with GWTEnt, right?

            Comment


              #36
              Originally posted by Isomorphic View Post
              Thanks for posting that, it may help someone else.

              Just in case - there's no way we could modify SmartGWT to avoid this problem with GWTEnt, right?
              I doubt there is something that can be done in SmartGWT. I believe this is likely either the GWTENT code or a GWT compilation problem.

              That being said, it would be great if someone would implement some kind of BeanUtil package for GWT.

              Comment


                #37
                Compilation error work around

                Originally posted by dbscott525 View Post
                The problem was with ENT's annotation class. Here is the code I had to implement on the annotation class:

                Code:
                	private String annotationToString(Annotation annotation) {
                
                		/**
                		 * IMPORTANT: The expression below is needed because apparently the code
                		 * generation of the GWTENT library prevents the use of String methods
                		 * directly on the annotation name. This is a GWTENT bug.
                		 * 
                		 * By concatenating the annotation name with a blank string a new string
                		 * is created. This new string allows String methods to be performed.
                		 */
                
                		return "" + annotation.toString();
                	}
                I should point out that I abandoned using GWTENT for publish/subscribe. Instead I implemented a home-grown way of doing this with an enum.

                I do use GWTENT for marshaling data between Java beans and SmartGWT records and criteria.
                Hi dbscott525,

                I also have the same problem discussed in this thread. Based on your suggestion, I changed the method in com.gwtent.reflection.client.ReflectionUtils.java insdie the gwtent.jar from:

                public static String annotationToString(Annotation anno){
                StringBuilder sb = new StringBuilder();

                sb.append(anno.annotationType().getName()).append("(");
                ClassType type = TypeOracle.Instance.getClassType(anno.annotationType());
                for (Method method : type.getMethods()){
                sb.append(method.getName()).append("=").append(method.invoke(anno)).append(";");
                }
                sb.append(")");

                return sb.toString();
                }

                to:

                private static String annotationToString(Annotation anno){
                return ""+anno.toString();
                }

                I still get the same compilation error. Is it the correct place I changed?

                btw: the gwtent.jar I downloaded is gwtent2.0 RC1(GWT2.3).zip from http://code.google.com/p/gwt-ent/downloads/list. Thanks.

                Comment


                  #38
                  Originally posted by xuyani View Post
                  Hi dbscott525,

                  I also have the same problem discussed in this thread. Based on your suggestion, I changed the method in com.gwtent.reflection.client.ReflectionUtils.java insdie the gwtent.jar from:

                  public static String annotationToString(Annotation anno){
                  StringBuilder sb = new StringBuilder();

                  sb.append(anno.annotationType().getName()).append("(");
                  ClassType type = TypeOracle.Instance.getClassType(anno.annotationType());
                  for (Method method : type.getMethods()){
                  sb.append(method.getName()).append("=").append(method.invoke(anno)).append(";");
                  }
                  sb.append(")");

                  return sb.toString();
                  }

                  to:

                  private static String annotationToString(Annotation anno){
                  return ""+anno.toString();
                  }

                  I still get the same compilation error. Is it the correct place I changed?

                  btw: the gwtent.jar I downloaded is gwtent2.0 RC1(GWT2.3).zip from http://code.google.com/p/gwt-ent/downloads/list. Thanks.
                  The code I showed you is my code outside of the GWTENT library.

                  Comment

                  Working...
                  X