How to send status to other class in JAVA - java

I have three classes serviceManager,serviceMasterHandler and WokerThread.serviceManager instantiate ServiceMasterHandler,ServiceMasterHandler instantiate and start workerThread.
I want to implements that each class started successfully it's send status to calling upper classes.
All ideas on how to implement is welcome.
Thanks

Have you already heard about PropertyChangeEvent and associated classes (like PropertyChangeListener and PropertyChangeSupport) ? If not, go take a look at them .. And read the chapter in Java tutorial about ProperttyChangeListener. Notice that, although it's defined in a Swing context, elements from java.beans can perfectly be used anywhere else, as they are not Swing-dependant.

Simple way is to have a status flag at each upperclasses and update it to started in the child classess in child's constructor or any init method if you have.

Related

PreferenceGroupAdapter can only be accessed from within the same library group

I am trying to use the PreferenceGroupAdapter adapter on my FragmentPreferenceCompat without luck. I am using the the code from the following thread:
https://stackoverflow.com/a/51832736
I get the message:
PreferenceGroupAdapter can only be accessed from within the same library group
Am I right to assume that this class was available at some point and they have decided to restrict it for internal use at a later stage? If that is the case, is there any way to access/iterate over the viewholders to perform a specific task.
Regards.
I think the class had restricted methods before.
#SuppressLint("RestrictedApi") above the class fixes that for me.

How to extend the CellSignalStrength class when it doesn't have a constructor?

There is a class called CellSignalStrength that does not have a default constructor.
To be able to use SignalStrength, I have to make a class that extracts it, but I can't because when I try to do that, it prints out the error:
No default contstructor available for SignalStrength.
Also, there is another class called CellSignalStrengthGsm(same problem), but that class extends SignalStrength class, but how?
I've done some research on this, and i couldn't find anything, but this guide is only offering the signal strength of neighboring cell-sites to me, none of them is the one providing me with the signal. That solution is offering no actual signal strength.
Why doesn't the Android documentation have at least one example for how to use the class?
I really need help on this, I'm stuck :/
The only reason I can think of why you can't extend the CellSignalStrength class is because its constructor is protected CellSignalStrength(). Meaning only other classes in the same package may extend it.
The Android developers probably had good reason to do so. My suggestion would be to rethink what you're trying to do and figure out another solution. Perhaps using an instance of one of the classes that sub-classes CellSignalStrength:
Note: These classes are all final, so don't try to extend them.
CellSignalStrengthCdma
CellSignalStrengthGsm
CellSignalStrengthLte
CellSignalStrengthWcdma

How can I avoid the MarshalException for Java RMI?

I just finished a small program with java rmi and somehow it doesn't work. Everytime I want to start the server I'm getting the MarshalException. Are there any important points that I should be aware of them on how to implement the interface for the remote method invocation? I thought it would be possible to create an implementation but also include some additional methods like a constructor or private variables inside of the implementing class.
Shouldn't this just work?
Greetings
In order to be able to transfer objects you need to make them implement Serializable. And perhaps have a default (no-arg) constructor (this is not a requirement for serialization though)
As helios noted, not only the class, but all your field hierarchy (classes of fields, and the classes of their fields) must be Serializable)
Caused by:
java.lang.ClassNotFoundException:
vsys.ue04.server.RemoteChargeImplementation
There's your problem. That class is required at the client.

Newbie Java developer questions - inter class communication with events

Please forgive the very basic nature of this questions - but we all have to start somewhere. I've done some googling but all answers seem to relate to UI Events.
I am creating a very simple android app that will display your location on screen. I have my main class (HelloAndroid at the moment) that extends Activity and I have created a class LcoationUpdateHandler that listens for updates.
HelloAndroid holds an instance of LocationUpdateHandler so my question is how does the LocationUpdateHandler communicate with HelloAndroid.
In flex I would dispatch an event from one to the other but from the searching I have done this doesn't seem like a very java-y way of doing things?
Thanks for your help.
When your HelloAndroid instance creates an instance of LocationUpdateHandler it can pass a reference to itself in the constructor, which LocationUpdateHandler can store to use for future method calls in the case of events.
For these kinds of situations you don't really need to know what type of object instatiated LocationUpdateHandler. This is were interfaces come in, you can define an interface defining the event methods and implement that interface so that LocationUpdateHandler can keep a reference to that interface to deliver events.
If the situation is symmetrical, both classes can implement the same event interface.
It sounds like what you're looking for is the Observer pattern. The way it works is that observers register with the object that they are observing, such that they can be notified on events.
In your specific case, if you want LocationUpdateHandler to push information to HelloAndroid, it has to know about HelloAndroid. So your LocationUpdateHandler should at least contain a reference to HelloAndroid, but to generalize this, it should have a List of observers that all implement a common interface containing a callback function that would be called whenever LocationUpdateHandler has an update.

In Java, how can I construct a "proxy wrapper" around an object which invokes a method upon changing a property?

I'm looking for something similar to the Proxy pattern or the Dynamic Proxy Classes, only that I don't want to intercept method calls before they are invoked on the real object, but rather I'd like to intercept properties that are being changed. I'd like the proxy to be able to represent multiple objects with different sets of properties. Something like the Proxy class in Action Script 3 would be fine.
Here's what I want to achieve in general:
I have a thread running with an object that manages a list of values (numbers, strings, objects) which were handed over by other threads in the program, so the class can take care of creating regular persistent snapshots on disk for the purpose of checkpointing the application. This persistor object manages a "dirty" flag that signifies whether the list of values has changed since the last checkpoint and needs to lock the list while it's busy writing it to disk.
The persistor and the other components identify a particular item via a common name, so that when recovering from a crash, the other components can first check if the persistor has their latest copy saved and continue working where they left off.
During normal operation, in order to work with the objects they handed over to the persistor, I want them to receive a reference to a proxy object that looks as if it were the original one, but whenever they change some value on it, the persistor notices and acts accordingly, for example by marking the item or the list as dirty before actually setting the real value.
Edit: Alternatively, are there generic setters (like in PHP 5) in Java, that is, a method that gets called if a property doesn't exist? Or is there a type of object that I can add properties to at runtime?
If with "properties" you mean JavaBean properties, i.e. represented bay a getter and/or a setter method, then you can use a dynamic proxy to intercept the set method.
If you mean instance variables, then no can do - not on the Java level. Perhaps something could be done by manipulations on the byte code level though.
Actually, the easiest way to do it is probably by using AspectJ and defining a set() pointcut (which will intercept the field access on the byte code level).
The design pattern you are looking for is: Differential Execution. I do believe.
How does differential execution work?
Is a question I answered that deals with this.
However, may I suggest that you use a callback instead? You will have to read about this, but the general idea is that you can implement interfaces (often called listeners) that active upon "something interesting" happening. Such as having a data structure be changed.
Obligitory links:
Wiki Differential execution
Wiki Callback
Alright, here is the answer as I see it. Differential Execution is O(N) time. This is really reasonable, but if that doesn't work for ya Callbacks will. Callbacks basically work by passing a method by parameter to your class that is changing the array. This method will take the value changed and the location of the item, pass it back by parameter to the "storage class" and change the value approipriately. So, yes, you have to back each change with a method call.
I realize now this is not what you want. What it appears that you want is a way that you can supply some kind of listener on each variable in an array that would be called when that item is changed. The listener would then change the corresponding array in your "backup" to refect this change.
Natively I can't think of a way to do this. You can, of course, create your own listeners and events, using an interface. This is basically the same idea as the callbacks, though nicer to look at.
Then there is reflection... Java has reflection, and I am positive you can write something using it to do this. However, reflection is notoriously slow. Not to mention a pain to code (in my opinion).
Hope that helps...
I don't want to intercept method calls before they are invoked on the real object, but
rather I'd like to intercept properties that are being changed
So in fact, the objects you want to monitor are no convenient beans but a resurgence of C structs. The only way that comes to my mind to do that is with the Field Access call in JVMTI.
I wanted to do the same thing myself. My solution was to use dynamic proxy wrappers using Javassist. I would generate a class that implements the same interface as the class of my target object, wrap my proxy class around original class, and delegate all method calls on proxy to the original, except setters which would also fire the PropertyChangeEvent.
Anyway I posted the full explanation and the code on my blog here:
http://clockwork-fig.blogspot.com/2010/11/javabean-property-change-listener-with.html

Categories

Resources