Not able to set Object (HashMap) at application level - java

I wanted to set a Hashmap object (of logged in users) at application level. But unable to do the same as with every request my old user list saved in Hashmap object is getting refreshed and returning only current logged in user.
I have tried many example but no luck.
Below is my code
<bean id="applicationContextProvder" class="com.starter.basic.ApplicationContextProvider"/>
<bean id="testBean" class="com.starter.basic.TestBean"/>
TestBean tb = appContext.getApplicationContext().getBean("testBean", TestBean.class)
public class ApplicationContextProvider implements ApplicationContextAware
{
private static ApplicationContext context;
public ApplicationContext getApplicationContext() {
return context;
}
#Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException {
context = ac;
}
}
public class TestBean implements ServletContextAware {
private HashMap hashMap = new HashMap<String ,String >();
}
public HashMap getUsers(){
return hashMap;
}
Framework used Spring MVC 3 + Hibernate 4.0
If anyone not able to understand my points above. What i am trying to do is if some user is already logged in and try to login again from another device user should blocked. I should get that user from Hashmap object on his second login. I could do this from back end flag. But my requirement is to use application context.

You should use application scope for your bean:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-application
You should add a user to a hashmap on every login and remove him on every logout.

You need Singleton Pattern for your HashMap and for the concurrency problem you need use ConcurrentHashMap to make sure thread safe,
You can use TestBean.getHashMap() to get, put or remove when you need:
class TestBean implements ServletContextAware {
private static final HashMap hashMap = new ConcurrentHashMap<String, String>();
public static HashMap getHashMap() {
return hashMap;
}
}
static variable will exist all the time when your program is in runtime.

Related

Do I need to synchronize #Autowired method in Spring when I want to save multiple beans in collection?

Let's imagine I have the next classes in the project based on Spring framework:
interface I {
String getName()
}
#Component
class I1 implements I {
#Override
String getName() {return "I1"}
}
#Component
class I2 implements I {
#Override
String getName() {return "I1"}
}
And I want to gather them all in the map using the #Autowired method:
#Component
public class A {
private Map<I> map = new HashMap<>()
#Autowired
public registerI(I i) {
map.put(i.getName(), i)
}
}
Should I make this method registerI synchronized? I mean, can Spring call this method in several threads simultaneously? Or this method will be called sequentially?
Thanks
You don't have to use synchronized because Spring bean initialization is single-threaded and thread-safe. You can think of gotchas like thread-scoped or lazy beans but for regular singleton beans initialization happens in one thread.
You might want to use synchronized to make sure that after registerI() method is called your object is safely published, although auto-wired constructor with final field is more readable.
#Component
public class A {
private final Map<String, I> map;
public A(List<I> list) {
map = list.stream().collect(Collectors.toMap(I::getName, i -> i));
}
}
You will get an exception during app startup because Spring cannot determine the correct implementation of interface "I" what you want to inject. You should use #Qualifier.
If you want to accomplish that scenario, this should be enough.
#Component
public static class A {
private Map<String,I> map = new HashMap<>();
public A(List<I> list) {
//map = list.stream().collect(Collectors.toMap(I::getName, x -> x));
for (I i : list) {
map.put(i.getName(), i);
}
}
}
You will end with only one value in the map.
The commented line works if there are not duplicate map keys.
You can autowire context and get all the interested beans from it in a #PostConstruct method and create a hashmap with it.
Or
If you want that Map to be shared amongst multiple classes, make it a #Bean
#Configuration
class SomeConfig{
#Autowire Context context;
#Bean(name = "mapBean")
public Map<String, MyCustomClassName1> mapBean() {
Map<String, MyCustomClassName1> map = new HashMap<>();
//populate the map here - from Context
return map;
}
}
Spring fills List by your beans. After you can create map in postConstruct
#Component
public class A {
#Autowired
private List<I> list;
#Autowired
private Map<String, I> map;
#PostConstruct
private void init(){
map = list.stream()
.collect(Collectors.toMap(I::getName, element->element);
}
}

Spring Bean is not initializing on Application startup

In my application i want to initialize the bean before hitting application url, and store the common values for drop-down. here it is declaration for bean
<beans:bean id="listService" class="com.system.beans.DropDownList"
init-method="populateMasterList" scope="application"/>
Bean:
public class DropDownList implements InitializingBean
{
private static final Logger logger = LoggerFactory.getLogger(DropDownList.class);
public static Map<String, Map<Integer, String>> listMap = new HashMap<String, Map<Integer, String>>();
#Autowired
private static SystemService systemService;
#Autowired(required = true)
#Qualifier(value = "systemService")
public void setSystemService(SystemService systemService)
{
this.systemService = systemService;
}
#PostConstruct
public static Map<String, Map<Integer, String>> populateMasterList()
{
logger.debug("Calling Institute Info Masters");
List<InstituteInfoMaster> masterList = systemService.listInstituteInfoMasters();
Map<Integer, String> masterMap = new HashMap<Integer, String>();
masterMap.put(0, "---Select---");
masterList.forEach((master) ->
{
masterMap.put(master.getListId(), master.getValue());
});
logger.debug("Created Map for List Masters");
listMap.put("infoList", masterMap);
return listMap;
}
public Map<String, Map<Integer, String>> getListMap()
{
return listMap;
}
public static void setListMap()
{
listMap = populateMasterList();
}
#Override
public void afterPropertiesSet() throws Exception
{
populateMasterList();
}
}
I observed that it does not initializes on Application startup. when i try to update the master by calling DropDownList.setListMap(); it gives NullPointerException. but if i calls the jsp page where i am calling the Map as ${listService.listMap['infoList']} it displays the Drop-down on jsp after it if i tries to save master it executes successfully.
it means when i calls the jsp page where i am showing drop-down that time only it initializing the bean not on Application startup.
The actual problem is that you are not accessing the Spring bean, but the class, staticly. When you use the bean, i.e. the listService instance, Spring will initalize it for you on first access.
You are calling a static method, but when this happens, the dependant beans are not populated. Autowiring works for instances (i.e. in non-static context), so systemService is null in your application.
Update: I have just realized this line:
#Autowired
private static SystemService systemService;
This is fundamentally wrong. You cannot autowire static fields, it makes absolutely no sense in Spring (or in any similar framework). Spring beans are instances, and the framework set the autowired fields to references to other spring beans.

Java Spring Recreate specific Bean

I want to re-create (new Object) a specific bean at Runtime (no restarting the server) upon some DB changes. This is how it looks -
#Component
public class TestClass {
#Autowired
private MyShop myShop; //to be refreshed at runtime bean
#PostConstruct //DB listeners
public void initializeListener() throws Exception {
//...
// code to get listeners config
//...
myShop.setListenersConfig(listenersConfig);
myShop.initialize();
}
public void restartListeners() {
myShop.shutdownListeners();
initializeListener();
}
}
This code does not run as myShop object is created by Spring as Singleton & its context does not get refreshed unless the server is restarted. How to refresh (create a new object) myShop ?
One bad way I can think of is to create new myShop object inside restartListeners() but that does not seem right to me.
In DefaultListableBeanFactory you have public method destroySingleton("beanName")so you can play with it, but you have to be aware that if your autowired your bean it will keep the same instance of the object that has been autowired in the first place, you can try something like this:
#RestController
public class MyRestController {
#Autowired
SampleBean sampleBean;
#Autowired
ApplicationContext context;
#Autowired
DefaultListableBeanFactory beanFactory;
#RequestMapping(value = "/ ")
#ResponseBody
public String showBean() throws Exception {
SampleBean contextBean = (SampleBean) context.getBean("sampleBean");
beanFactory.destroySingleton("sampleBean");
return "Compare beans " + sampleBean + "=="
+ contextBean;
//while sampleBean stays the same contextBean gets recreated in the context
}
}
It is not pretty but shows how you can approach it. If you were dealing with a controller rather than a component class, you could have an injection in method argument and it would also work, because Bean would not be recreated until needed inside the method, at least that's what it looks like. Interesting question would be who else has reference to the old Bean besides the object it has been autowired into in the first place,because it has been removed from the context, I wonder if it still exists or is garbage colected if released it in the controller above, if some other objects in the context had reference to it, above would cause problems.
We have the same use-case. As already mentioned one of the main issues with re-creating a bean during runtime is how to updating the references that have already been injected. This presents the main challenge.
To work around this issue I’ve used Java’s AtomicReference<> class. Instead of injecting the bean directly, I’ve wrapped it as an AtomicReference and then inject that. Because the object wrapped by the AtomicReference can be reset in a thread safe manner, I am able to use this to change the underlying object when a database change is detected. Below is an example config / usage of this pattern:
#Configuration
public class KafkaConfiguration {
private static final String KAFKA_SERVER_LIST = "kafka.server.list";
private static AtomicReference<String> serverList;
#Resource
MyService myService;
#PostConstruct
public void init() {
serverList = new AtomicReference<>(myService.getPropertyValue(KAFKA_SERVER_LIST));
}
// Just a helper method to check if the value for the server list has changed
// Not a big fan of the static usage but needed a way to compare the old / new values
public static boolean isRefreshNeeded() {
MyService service = Registry.getApplicationContext().getBean("myService", MyService.class);
String newServerList = service.getPropertyValue(KAFKA_SERVER_LIST);
// Arguably serverList does not need to be Atomic for this usage as this is executed
// on a single thread
if (!StringUtils.equals(serverList.get(), newServerList)) {
serverList.set(newServerList);
return true;
}
return false;
}
public ProducerFactory<String, String> kafkaProducerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.CLIENT_ID_CONFIG, "...");
// Here we are pulling the value for the serverList that has been set
// see the init() and isRefreshNeeded() methods above
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, serverList.get());
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<>(configProps);
}
#Bean
#Lazy
public AtomicReference<KafkaTemplate<String, String>> kafkaTemplate() {
KafkaTemplate<String, String> template = new KafkaTemplate<>(kafkaProducerFactory());
AtomicReference<KafkaTemplate<String, String>> ref = new AtomicReference<>(template);
return ref;
}
}
I then inject the bean where needed, e.g.
public MyClass1 {
#Resource
AtomicReference<KafkaTemplate<String, String>> kafkaTemplate;
...
}
public MyClass2 {
#Resource
AtomicReference<KafkaTemplate<String, String>> kafkaTemplate;
...
}
In a separate class I run a scheduler thread that is started when the application context is started. The class looks something like this:
class Manager implements Runnable {
private ScheduledExecutorService scheduler;
public void start() {
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(this, 0, 120, TimeUnit.SECONDS);
}
public void stop() {
scheduler.shutdownNow();
}
#Override
public void run() {
try {
if (KafkaConfiguration.isRefreshNeeded()) {
AtomicReference<KafkaTemplate<String, String>> kafkaTemplate =
(AtomicReference<KafkaTemplate<String, String>>) Registry.getApplicationContext().getBean("kafkaTemplate");
// Get new instance here. This will have the new value for the server list
// that was "refreshed"
KafkaConfiguration config = new KafkaConfiguration();
// The set here replaces the wrapped objet in a thread safe manner with the new bean
// and thus all injected instances now use the newly created object
kafkaTemplate.set(config.kafkaTemplate().get());
}
} catch (Exception e){
} finally {
}
}
}
I am still on the fence if this is something I would advocate doing as it does have a slight smell to it. But in limited and careful usage it does provide an alternate approach to the stated use-case. Please be aware that from a Kafka standpoint this code example will leave the old producer open. In reality one would need to properly do a flush() call on the old producer to close it. But that's not what the example is meant to demonstrate.

How to create a MAP that has an application scope? And Where to declare it?

I need to create a Map that has application scope. And so, if user1 add to this Map an object using method1 of class1, user2 would find the new objects using method2 of class2.
I know there is this annotation :
#ApplicationScoped
But, I don't know where my map should be declared or used, to make it have the same state at anytime and by anywhere in the application deployment time.
An example representing a class where this Map is declared and a method of another class using it, would be so helpful.
Declare a CDI bean that will provide this Map for its consumption:
#Named
#ApplicationScoped
public class ApplicationScopedBean {
private Map<KeyClass, ValueClass> map;
#PostConstruct
public void init() {
//initialize the map and its data here
map = new ConcurrentHashMap<>();
map.put(..., ...);
//...
}
//provide a getter for the map
public Map<KeyClass, ValueClass> getMap() {
return this.map;
}
}
Now, the bean can be injected in clients and can show the data in your view.
I'm not sure about your experience with Java language. But why don't you create a static variable in class?
So, for example:
class A {
public static Map<String, String> globalMap;
}
From class B you could access or set it anywhere(ofcourse you need to import the class A at the top):
class B {
public void doAnything(){
Map anyMap<String, String> anyMap = new HashMap<String, String>();
anyMap.put("anyString", "anyString");
A.globalMap = anyMap;
}
}

How can I select Spring bean instance at runtime

Based on parameters passed to a method, I need to select from one of many Spring beans that are implementations of the same class, but configured with different parameters.
E.g. if user A invokes the method, I need to call dooFoo() on bean A, but if it's user B then I need to call the very same method, only on bean B.
Is there a 'Springier' way of doing this other than sticking all the beans in a map, and deriving a key from the parameters passed to my method?
We face that issue in our project, and we solve it through a Factory-Like class. The client class -the one that needed the bean at runtime- had an instance of the factory, that was injected through Spring:
#Component
public class ImTheClient{
#Autowired
private ImTheFactory factory;
public void doSomething(
Parameters parameters) throws Exception{
IWantThis theInstance = factory.getInstance(parameters);
}
}
So, the IWantThis instance depends on the runtime value of the parameters parameter. The Factory implementation goes like this:
#Component
public class ImTheFactoryImpl implements
ImTheFactory {
#Autowired
private IWantThisBadly anInstance;
#Autowired
private IAlsoWantThis anotherInstance;
#Override
public IWantThis getInstance(Parameters parameters) {
if (parameters.equals(Parameters.THIS)) {
return anInstance;
}
if (parameters.equals(Parameters.THAT)) {
return anotherInstance;
}
return null;
}
}
So, the factory instance holds reference to both of the posible values of the IWantThis class, being IWantThisBadly and IAlsoWantThis both implementations of IWantThis.
Seems like do you want a ServiceLocator using the application context as registry.
See ServiceLocatorFactoryBean support class for creating ServiceLocators mapping keys to bean names without coupling client code to Spring.
Other option is to use a naming convention or annotation based configuration.
for example, assuming that you annotate Services with #ExampleAnnotation("someId"), you can use something like the following Service Locator to retrieve them.
public class AnnotationServiceLocator implements ServiceLocator {
#Autowired
private ApplicationContext context;
private Map<String, Service> services;
public Service getService(String id) {
checkServices();
return services.get(id);
}
private void checkServices() {
if (services == null) {
services = new HashMap<String, Service>();
Map<String, Object> beans = context.getBeansWithAnnotation(ExampleAnnotation.class);
for (Object bean : beans.values()) {
ExampleAnnotation ann = bean.getClass().getAnnotation(ExampleAnnotation.class);
services.put(ann.value(), (Service) bean);
}
}
}
}
Sticking them in a map sounds fine. If it's a Spring-managed map (using util:map, or in Java config), that's better than creating it somewhere else, because then Spring owns all the object references and can manage their lifecycle properly.
If the beans (A, B) you are talking about are SessionScope its no problem at all, they will be selected correctly.
public class BusinessLogic {
private BaseClassOfBeanAandB bean;
public void methodCalledByUserAorB() {
bean.doFoo();
}
}

Categories

Resources