Java enum fields serialization/deserialization -
intro
i using apache storm (local mode, not remote mode) in java project , when creating topology need pass object 1 of bolts
topologybuilder builder = new topologybuilder(); ..... builder.setbolt("last-bolt", new mybolt(classifier.seconds)).somegrouping(...); ..... localcluster cluster = new localcluster(); cluster.submittopology("test", conf, builder.createtopology());
the object has non-serializable fields. instead of subclassing classes fields belong , making them serializable have taken approach. since actual object isn't gonna changing lot , can enumerated i've decided create enum , pass bolt's tasks. thing enum is serializable under costs. approach works in local mode because (if understood storm correctly) there 1 jvm running on computer , things can't complicated actually.
question
if enum consists of static final non-serializable field field constructed when enum deserialized process on different machine or cluster running multiple jvms?
the actual enum (static final field @ end)
public enum classifier { seconds { public string classify(string timestamp) { datetime datetime = formatter.parsedatetime(timestamp); int sec = datetime.getsecondofminute(); if (second <= 30) { homecoming "00 - 30"; } else { homecoming "30 - 60"; } } public int getnumberofcategories() { homecoming 2; } }, week { public string classify(string timestamp) { datetime datetime = formatter.parsedatetime(timestamp); int dayofweek = datetime.getdayofweek(); string typeofday = (dayofweek >= 1 && dayofweek <= 5) ? "workday" : "weekend"; int hr = datetime.gethourofday(); string hourinterval = hr + " - " + (hour == 23 ? 0 : hr + 1); homecoming typeofday + " " + hourinterval; } public int getnumberofcategories() { homecoming 48; } }; private static final datetimeformatter formatter = datetimeformat.forpattern("yyyy-mm-dd hh:mm:ss"); public abstract string classify(string timestamp); public abstract int getnumberofcategories(); }
more details
datetimeformatter , datetime org.joda.time package.
all static final
fields initialized when class loaded. whatever serialization mechanism used first initialize static fields , execute static initialization blocks. note static fields not deserialised because not deserialising classes objects (please refer reply http://stackoverflow.com/a/6429497/1937263).
so reply yes, field should constructed properly.
java serialization static enums apache-storm
No comments:
Post a Comment