Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday, April 1, 2017

MVP : Event Bus pattern instead of Listener

This question more towards paradigm. Why is that we don't use Event Bus instead of listeners in an MVP environment ? Typically, the "P" part has dependency injection of view and model references. Sure this has an advantage of showing explicit contract between the view and model via presenter, which is more readable.
However, wouldn't it be a cleaner approach to have presenter listen to events from views and events carry the view payload (eg: json representation). Same is the case with presenter talking back to the view. View will listen for events from presenter. Major advantage is, we don't have to write interfaces for each contract between view and presenter. If you look at the code you will see that presenter is getting exposed to view details like Text Fields, which i believe is increasing coupling between view and presenter. Say, if i'm replacing front end JavaFx instead of Vaadin, i will have to alter presenter as well.
This class is an example from a live project. Here we have different types of events ie i don't create event class for different cases. Eg: LoginViewEvent , DashBoardEvent etc which i believe is a maintenance pain.
public class UrayEvent {

    public static enum EventType {

        SESSION_SELECTED(1),
        DOCUMENT_SELECTED(2),
        DOCUMENT_EDIT_COMPLETE(3),
        DOCUMENT_EDIT_CANCELED(4),
        SHOW_SESSION_TABLES(5),
        SHOW_SESSION_DOCUMENTS(6),
        SHOW_SESSION_COLLABORATORS(7),
        USER_REQUESTED_REFRESH(8),
        AUTO_REFRESH(9),
        TABLE_SELECTED(10),
        DETACHED(11),
        SCHEDULER_NAVIGATION(12),
        JIRA_USER_SELECTED(13),
        DOCUMENT_SAVE_SUCCESS(14),
        DOCUMENT_SAVE_FAILURE(14);

        private final int value;

        private EventType(int value) {

            this.value = value;
        }

        public int getValue() {

            return value;
        }
    }

    public static class Event {

        private final EventType type;
        private final Object payload;

        public Event(EventType type, Object eventPayload) {

            this.type = type;
            this.payload = eventPayload;
        }

        public EventType getEventType() {

            return type;
        }

        public Object getEventPayload() {

            return payload;
        }
    }

}
Simple enough, the view send the event  DOCUMENT_EDIT_COMPLETE .The presenter layer handles this event. I found this way, a better way to decouple views from presenter.
    @Subscribe
    public void handle(UrayEvent.Event event) {

        switch (event.getEventType()) {
            case DOCUMENT_EDIT_COMPLETE:
                  // event payload contains document model data
                  // like document id etc
                 saveDocument(event.getEventPayload);    
                break;
            default:
                break;
        }
    }
Advantage
  • Less boiler plate code, 
  • For n-views we don't need n-interfaces
  • New event means adding event element to enum and updating respective subscribe methods handling this event.
Disadvantage
  • Memory leak if we forget to unregister  from eventbus (faced it plenty of time)
Questions
1) This approach means, there would larger set enum elements as the application grow. Is this approach an anti pattern ?
2) As we saw it uses Event Bus extensively are there any drawbacks of using bus system instead of interface-listener pattern ?
Wanted your valuable suggestion on this regard. The main issue is, if i blindly apply this pattern extensively across the project i shouldn't regret doing so, what would be a possible pitfall in this approach.

Wednesday, February 15, 2017

Java : convert milliseconds hours, minutes, seconds ,days

An example using built in  TimeUnit .
                            long uptime = System.currentTimeMillis();

                            long days = TimeUnit.MILLISECONDS
                                    .toDays(uptime);
                            uptime -= TimeUnit.DAYS.toMillis(days);

                            long hours = TimeUnit.MILLISECONDS
                                    .toHours(uptime);
                            uptime -= TimeUnit.HOURS.toMillis(hours);

                            long minutes = TimeUnit.MILLISECONDS
                                    .toMinutes(uptime);
                            uptime -= TimeUnit.MINUTES.toMillis(minutes);

                            long seconds = TimeUnit.MILLISECONDS
                                    .toSeconds(uptime);

uptime:22hr 15min 40sec 14 days


Monday, February 13, 2017

Android and Microsoft SQL (MS SQL SERVER 2008) marriage

All you have to do is use the appropriate driver, being a opensource fan , i'd recommend using JTDS.

Instructions below assumes you are using





  • Android Developer Tools Build id: v22.0.1-685705,
  • Eclipse Version: 4.2.1.v20130118 Build id: M20130204-1200
  • JTDS 1.2.5 

  • Though things will work out other versions higher than this and possibly lower as well , its just to ensure a known state where things worked out.

    So, once the JTDS is jar is download, first step is to include jar to your build path





    Next, it seems like a bug, go to order and Export tab and enable jtds




    Since the database connection operation is networked related, all network operations should be in different thread away from the main UI thread,  so use  AsyncTask.

    Working code is can be downloaded from here , read the code, it is well documented.


    git@github.com:alkber/AndroidByExample.git
    

    Monday, September 17, 2012

    Local Data address Does not belong to any of this hosts local interfaces


    byte[] ip = new byte[4];
            ip[0] = (byte) 192;
            ip[1] = (byte) 168;
            ip[2] = (byte) 1;
            ip[3] = (byte) 8;
    SessionAddress localSessionAddr = new SessionAddress(
                    InetAddress.getByAddress(ip), LOCAL_RTP_PORT);
    
    Throws: Local Data Address Does not belong to any of this hosts local interfaces

    Solution and Cause


    The problem is JMF seems to use InetAddress.getAllByName() which gives (in most cases) only single IP address as it may not be in the /etc/hosts. It can be sorted out by setting your interface ip (say eth0, wlan0) in /etc/hosts [1].
    >  cat /etc/hosts> 127.0.0.1 localhost> 127.0.1.1 noor> 192.168.1.8 noor