I build a that contain some objects like that:
public class TestThis implements Listener{
private EventsManager eventsManager;
private ConfigManager configManager;
public TestThis() {
eventsManager = new EventsManager();
eventsManager.addListener(this);
configManager = new ConfigManager(this);
}
public ConfigManager getConfigManager() {
return configManager;
}
#Override
public void configLoadedSuccessfully(Event event) {
System.out.println(this.configManager); //OUTPUT NULL
System.out.println(this.getConfigManager());//OUTPUT NULL
}
}
There is problem with the configManager object.
In the construct everything is ok but is configLoadedSuccessfully event the configManager is null and its null evrywhere.
The Listener Code:
public interface Listener{
public void configLoadedSuccessfully(Event event);
}
I add a function that start loading the config and change the order of the var`s create.
public class TestThis implements Listener{
private EventsManager eventsManager;
private ConfigManager configManager;
public TestThis() {
eventsManager = new EventsManager();
configManager = new ConfigManager(this);
eventsManager.addListener(this);
configManager.load();
}
public ConfigManager getConfigManager() {
return configManager;
}
#Override
public void configLoadedSuccessfully(Event event) {
System.out.println(this.configManager);
System.out.println(this.getConfigManager());
}
}
Related
Help me in the following code and how to used the backup on the Hazelcast
migration of the hazelcast 3.x.x to 5.x.x
package com.hazelcast.map;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastInstanceAware;
import com.hazelcast.nio.serialization.impl.BinaryInterface;
import java.util.Map;
// Interface AbstractEntryProcessor
#BinaryInterface
public abstract class AbstractEntryProcessor<K,V> implements EntryProcessor<K,V> {
private final EntryBackupProcessor<K,V> entryBackupProcessor;
// Non Parameterize Constructor
public AbstractEntryProcessor() {
this(true);
}
// Parameterize Constructor AbstractEntryProcessor
public AbstractEntryProcessor(boolean applyOnBackup) {
if (applyOnBackup) {
entryBackupProcessor = new EntryBackupProcessorImpl();
} else {
entryBackupProcessor = null;
}
}
//EntryBackupProcessor
#Override
public final EntryBackupProcessor getBackupProcessor() {
return entryBackupProcessor;
}
// class EntryBackupProcessorImpl
private class EntryBackupProcessorImpl implements EntryBackupProcessor<k,V>, HazelcastInstanceAware {
// generated for EntryBackupProcessorImpl which doesn't implement HazelcastInstanceAware
static final long serialVersionUID = -5081502753526394129L;
#Override
public void processBackup(Map.Entry<K,V> entry) {
process(entry);
}
#Override
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
final AbstractEntryProcessor<k,V> outer = AbstractEntryProcessor.this;
if (outer instanceof HazelcastInstanceAware) {
((HazelcastInstanceAware) outer).setHazelcastInstance(hazelcastInstance);
}
}
}
}
How to used the backup methods in 5.x.x versons of series
how to used the backup in the above question ?
This should work:
public abstract class AbstractEntryProcessor implements EntryProcessor, HazelcastInstanceAware {
protected transient HazelcastInstance hazelcastInstance;
private final boolean applyOnBackup;
// Non Parameterize Constructor
public AbstractEntryProcessor() {
this(true);
}
// Parameterize Constructor AbstractEntryProcessor
public AbstractEntryProcessor(boolean applyOnBackup) {
this.applyOnBackup = applyOnBackup;
}
//EntryBackupProcessor
#Override
public final EntryProcessor getBackupProcessor() {
if (!applyOnBackup || this instanceof ReadOnly) {
return null;
}
return this;
}
#Override
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}
}
I have two beans of the same class that I want to listen each for a bean-specific but generic event:
public class MyBeanClass <E> {
#EventListener
public void handleEvent(E event) { ... }
}
Config:
public class MyConfig {
#Bean
public MyBeanClass<AEvent> myBeanAClass() {
return new MyBeanClass<>();
}
#Bean
public MyBeanClass<BEvent> myBeanBClass() {
return new MyBeanClass<>();
}
}
So bean "myBeanAClass" shall listen for AEvent's and bean "myBeanBClass" shall listen for BEvent's.
Test:
#Test
public void testHandleAEvent() {
AEvent event = new AEvent();
publisher.publishEvent(event);
Mockito.verify(myBeanAClass, times(1)).handleEvent(Mockito.any()); // Fail
Mockito.verify(myBeanBClass, times(0)).handleEvent(Mockito.any());
}
Error:
org.mockito.exceptions.verification.TooManyActualInvocations:
mypackage.MyBeanClass#0 bean.handleEvent(
<any>
);
Wanted 1 time:
-> at mypackage.MyTest.testHandleAEvent(MyTest.java:45)
But was 5 times:
-> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
-> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
-> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
-> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
-> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Because of Type Erasure the generic type E in handleEvent(E event) will be replaced with Object. After replacement class will look:
public class MyBeanClass {
#EventListener
public void handleEvent(Object event) { ... }
}
It means that such listener will accept any events from the application even generated by the spring framework internally. The method signature declares the event type it consumes. EventListener documentation
Solution 1. Create listener adapter foreach event
Adapters for base generic listener:
public class MyBeanClass <E> {
public void handleEvent(E event) {
event.toString();
}
}
public class MyBeanAClass {
private MyBeanClass<AEvent> myBeanClass;
public MyBeanAClass(MyBeanClass<AEvent> myBeanClass) {
this.myBeanClass = myBeanClass;
}
#EventListener
public void handleEvent(AEvent event) {
myBeanClass.handleEvent(event);
}
}
public class MyBeanBClass {
private MyBeanClass<BEvent> myBeanClass;
public MyBeanBClass(MyBeanClass<BEvent> myBeanClass) {
this.myBeanClass = myBeanClass;
}
#EventListener
public void handleEvent(BEvent event) {
myBeanClass.handleEvent(event);
}
}
Events:
public class AEvent extends ApplicationEvent {
private final String message;
public AEvent(Object source, String message) {
super(source);
this.message = message;
}
}
public class BEvent extends ApplicationEvent {
private final String message;
public BEvent(Object source, String message) {
super(source);
this.message = message;
}
}
Config:
public class MyConfig {
#Bean
public MyBeanAClass myBeanAClass() {
return new MyBeanAClass(new MyBeanClass<>());
}
#Bean
public MyBeanBClass myBeanBClass() {
return new MyBeanBClass(new MyBeanClass<>());
}
}
Test:
class ApplicationTests {
#MockBean
private MyBeanAClass myBeanAClass;
#MockBean
private MyBeanBClass myBeanBClass;
#Autowired
private ApplicationEventPublisher applicationEventPublisher;
#Test
public void testHandleAEvent() {
AEvent event = new AEvent(this, "Message");
applicationEventPublisher.publishEvent(event);
Mockito.verify(myBeanAClass, times(1)).handleEvent(Mockito.any());
Mockito.verify(myBeanBClass, times(0)).handleEvent(Mockito.any());
}
}
Solution 2. Generic Application Event
Create a generic event type. Implement org.springframework.core.ResolvableTypeProvider in generic event class, then listener will resolve it.
public class GenericSpringEvent<T> implements ResolvableTypeProvider {
private final T source;
public GenericSpringEvent(T source) {
this.source = source;
}
public T getSource() {
return source;
}
#Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(
getClass(),
ResolvableType.forInstance(this.source)
);
}
}
Implement generic listeners for each event
public class GenericSpringAEventListener {
#EventListener
public void handle(GenericSpringEvent<AEvent> event) {
event.toString();
}
}
public class GenericSpringBEventListener {
#EventListener
public void handle(GenericSpringEvent<BEvent> event) {
event.toString();
}
}
Config:
public class MyConfig {
#Bean
public GenericSpringAEventListener genericSpringAEventListener() {
return new GenericSpringAEventListener();
}
#Bean
public GenericSpringBEventListener genericSpringBEventListener() {
return new GenericSpringBEventListener();
}
}
Test:
class ApplicationTests {
#MockBean
private GenericSpringAEventListener aListener;
#MockBean
private GenericSpringBEventListener bListener;
#Autowired
private ApplicationEventPublisher applicationEventPublisher;
#Test
public void testHandleAEvent() {
AEvent event = new AEvent(this, "Message");
GenericSpringEvent<AEvent> genericEvent = new GenericSpringEvent<>(event);
applicationEventPublisher.publishEvent(genericEvent);
Mockito.verify(aListener, times(1)).handle(Mockito.any());
Mockito.verify(bListener, times(0)).handle(Mockito.any());
}
}
I have a problem in which i am downloading some data in my background Service which is updating AppSingleton class which further notify the registered Observers in it.
Is Registering Observer via Singleton is preferred or that via RxJava.
AppSingleton looks like this:
public class AppGlobalData implements AppObservable {
private static final AppGlobalData ourInstance = new AppGlobalData();
private List<Customer> customerList;
private List<AppObserver> appObservers=new ArrayList<>();
public static synchronized AppGlobalData getInstance() {
return ourInstance;
}
private AppGlobalData() {
}
#Override
public void register(AppObserver obj) {
appObservers.add(obj);
}
#Override
public void unregister(AppObserver obj) {
appObservers.remove(obj);
}
#Override
public void notifyObservers() {
for (AppObserver observer : appObservers)
observer.update();
}
public List<Customer> getCustomerList() {
return customerList;
}
public void setCustomerList(List<Customer> customerList) {
this.customerList = customerList;
notifyObservers();
}
}
I am implementing a command pattern in android.
This is what I have right now. For some reason this does not run. It is like the AddUserRequest is getting garbage collected for some reason.
RequestManager.java:
public class RequestManager extends BroadcastReceiver {
private static final RequestManager instance = new RequestManager();
private boolean isConnected = false;
private static ArrayList<Request> requestQueue = new ArrayList<Request>();
private RequestManager() {
}
/* singleton class */
public static RequestManager getInstance() {
return instance;
}
public void invokeRequest(Request request) {
request.execute(); // only to test this, this will change
return;
}
}
AddUserRequest.java
public class AddUserRequest extends InsertionRequest {
User user;
public AddUserRequest(User user) {
this.user = user;
}
public void execute() {
System.out.println("TEST!!!");
}
}
Request.java:
public abstract class Request {
public abstract void execute();
}
}
InsertionRequest.java
public abstract class InsertionRequest extends Request {
}
RequestManagerTest.java
public class RequestManagerTest extends ActivityInstrumentationTestCase2 {
public RequestManagerTest(){
super(MainActivity.class);
}
public void testAddUserRequest() {
User user = new User();
user.setName("Tester12345");
AddUserRequest request = new AddUserRequest(user);
RequestManager.getInstance().invokeRequest(request);
}
}
For some reason this does not print "TEST!!!" and for the life of me I cannot figure out why. I looked in the debug log and everytime request.execute() in RequestManager.java gets called there is a "GC Explicit..." which I suspect has to do with garbage collection. What is the proper way to do what I am trying to do?
Is it possible to define following in Java:
public interface IGenericRepo<T> {
void add();
void delete();
void attach();
}
public interface IGenericRepo<Book> {
default String bookSpecificMethod(){
return "smthn";
}
}
public class NHGenericRepo<T> implements IGenericRepo<T>{
/* implementation */
}
public class NHUnitOfWork implements UnitOfWork{
#Autowired
public void setBookRepo(NHGenericRepo<Book> bookRepo) {
this.bookRepo= bookRepo;
}
public NHGenericRepo<Book> getBookRepo() {
return bookRepo;
}
private NHGenericRepo<Book> bookRepo;
}
And to be able somewhere in code to have:
{
#Autowired
public void setNhuw(NHUnitOfWork nhuw) {
this.nhuw = nhuw;
}
private NHUnitOfWork nhuw;
/**/
{
String st = this.nhuw.getBookRepo().bookSpecificMethod();
}
}
In .net this is possible by using Extension Method with "this IGenericRepo<Book>" as a first method parameter.
The closest you can come is:
public interface IBookGenericRepo extends IGenericRepo<Book> {
void BookSpecificMethod();
}