#Autowired not working inside a runnable - java

I have a runnable task where i am trying to Autowire field but when i do it the task doesn't run .
When i Autowire the field outside the runnable it works fine . Why is this happening ? Also is there any other cleaner way to get new instances of a autowired field inside runnable other than Autowiring it inside ?
Here's my runnable method `
Runnable task = new Runnable() {
#Autowired
ICruxPanelClientService CruxPanelClientService;
public void run (){
CruxPanelClientService.setCruxWebServiceBaseURL("http://10.41.181.23:8080");
CronCruxModel m = new CronCruxModel();
m = model1.get(model_var);
System.out.println("EXECUTING");
System.out.println(m.getService_status() + " ---------EXEexecution");
System.out.println(m.getCat_name() + "Executing Name ");
// time = m.getService_time();
UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest();
CategoryModel categoryModel = new CategoryModel();
categoryModel.setColor(m.getCat_color());
categoryModel.setIcon(m.getCat_icon());
categoryModel.setIconWhite(m.getCat_icon_white());
categoryModel.setName(m.getCat_name());
categoryModel.setId(m.getCat_id());
categoryModel.setKey(m.getCat_catkey());
categoryModel.setIndexOrder(m.getCat_indexOrder());
updateCategoryRequest.setCategory(categoryModel);
CruxPanelClientService.updateCategory(updateCategoryRequest);
GetServiceDataIdByCategoryIdRequest request1 = new GetServiceDataIdByCategoryIdRequest();
request1.setId(m.getCat_id());
GetServiceDataIdByCategoryIdResponse response1 = CruxPanelClientService.getServiceDataIdByCategoryId(request1);
ArrayList<ServiceModel> service = new ArrayList<ServiceModel>();
service = response1.getServiceModels();
JSONArray json = new JSONArray();
if(m.getService_order_succ_msg()==null)
{
json = new JSONArray();
}
else {
json = new JSONArray(m.getService_order_succ_msg());
}
String message = m.getService_order_succ_msg();
for (int j=0;j<service.size();j++)
{
UpdateServiceMasterRequest req = new UpdateServiceMasterRequest();
ServiceModel s = new ServiceModel();
s=service.get(j);
;
JSONObject obj = new JSONObject();
if(json.length()==0 )
{
String ms = null;
s.setOrderSuccessMessage(ms);
req.setServiceModel(s);
}
else {
String message1 = json.get(j).toString();
if(message1.equals(null) || message1.equals("")) {
String ms = null;
s.setOrderSuccessMessage(ms);
req.setServiceModel(s);
}
else {
s.setOrderSuccessMessage(message1);
req.setServiceModel(s);
}
}
CruxPanelClientService.updateServiceMaster(req);
}
m.setService_status("executed");
UpdateCronCruxRequest q = new UpdateCronCruxRequest();
q.setCronCruxModel(m);
CruxPanelClientService.updateCronCrux(q);
}
};`

The problem is spring doesn't control creation of your runnable. There are couple possible solutions:
Put you runnable creation in some service, repository, controller, component or what ever handled by spring:
Example:
#Service
public class SomeService {
#Autowired
private ICruxPanelClientService cruxPanelClientService;
public Runnable newRunnable() {
return new Runnable() {
public void run() {
cruxPanelClientService <- will be visible here and injected
}
}
}
}
Create runnable as bean with prototype scope
Example:
#Configuration
public class Runnableconfiguration {
#Bean
#Scope("prototype")
public Runnable newRunnbale(final ICruxPanelClientService cruxPanelClientService) {
return new Runnable() {
public void run() {
cruxPanelClientService <- will be visible here
}
}
}
}

#Autowire can't be used for anonymous classes (because you call new, not Spring), you can autowire a field in outer class and then use that field inside your Runnable.
Or make the Runnable a full blown class (not anonymous) and make it a bean (and autowire, e.g. using the id)

Spring does not #autowire anything into unmanaged instances (instances you create with new, in contrast to instances created and managed by Spring).
This leaves you following options:
#autowire the dependency outside and pass it to the Runnable (or make it accessible to the Runnable).
create a new class subclassing Runnable, and #autowire it where you need it.
Instead of having the dependency injected, look it up (applicationContext.getBean(..)) when needing it.
Be careful with scopes and lazy initialization when using Threads, as some of the Spring scopes (e.g. "request" or "session" scope) are thread-local bound (e.g. when using a lazy request-scoped dependency in a new Thread, which when executed is no longer associated with the current request).

In addition to all mentioned above, maybe it is better to inject the service from outside your runnable and not use #Autowired inside your runnable. So in your case:
Runnable task = new Runnable() {
#Autowired
ICruxPanelClientService CruxPanelClientService;
would become:
#Autowired
private ICruxPanelClientService CruxPanelClientService;
Runnable task = new Runnable() {
...
}

Related

Spring sleuth not creating new traceid inside a spring scheduler

I am scheduling the spring scheduler with SchedulingConfigurer as follows. However, new traceid is not getting created every time the "ProcessJob" method is getting called.
Even following method always logs with the same traceid.
log.info("Performing task");
What is the issue here? and how do i ensure new traceid everytime this job is triggered.
I have even tried wrapping "processJob" method call inside newSpan as follows: but no luck.
Fix 1: not working:
private void setSchedule() {
future =
taskScheduler.schedule(
() -> {
Span newSpan = tracer.nextSpan().name("newSpan").start();
try (SpanInScope ws = tracer.withSpanInScope(newSpan.start())) {
log.info("Performing task");
taskManager.processJob();
} finally {
newSpan.finish();
}
},
dynamicTrigger);
}
Original class that needs fix:
public class SchedulerConfig
implements SchedulingConfigurer, ApplicationListener<RefreshScopeRefreshedEvent> {
private final DynamicTrigger dynamicTrigger;
private final TaskManager taskManager;
private TaskScheduler taskScheduler;
private ScheduledFuture<?> future;
#Bean(destroyMethod = "shutdown")
public ExecutorService taskExecutor() {
return Executors.newScheduledThreadPool(1);
}
#Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
taskScheduler = taskRegistrar.getScheduler();
setSchedule();
}
private void setSchedule() {
future =
taskScheduler.schedule(
() -> {z
log.info("Performing task");
taskManager.processJob();
},
dynamicTrigger);
}
#Override
public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
log.info("Rescheduling due to change in cron expression");
future.cancel(false);
setSchedule();
}
The way you start the span is not how you suppose to do it (e.g.: you call start twice). Please check the docs to see how to do it properly: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#using-creating-and-ending-spans
The easiest way to start a new span is using #NewSpan on a method that belongs to a Spring Bean, please see the docs: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#using-annotations-new-spans
For scheduling, I think it is way simpler using #Scheduled, see the docs: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#sleuth-async-scheduled-integration
This is also instrumented out of the box by Sleuth so you don't need to do anything to start a new Span:
#Scheduled(fixedDelay = 1_000)
public void scheduled() {
log.info("Hey, look what I'm doing");
}
If you don't want to use #Scheduled, you can use a TraceableScheduledExecutorService as your ExecutorService, docs: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#sleuth-async-executor-service-integration

Accessing async method parameters or method's annotations from task

I have the below classes (simplified) to achieve async method calls using Java and Spring toolbox. I need to add some logic which is needed to execute before and after async method call.
Callable. I can put the logic i need here if can access data.
public class ContextAwareCallable<T> implements Callable<T> {
private Callable<T> task;
private MyContext context;
public ContextAwareCallable(Callable<T> task, MyContext context) {
this.task = task;
this.context = context;
}
#Override
public T call() throws Exception {
return task.call();
}
}
This is executor, where task is called.
public class ContextAwarePoolExecutor extends ThreadPoolTaskExecutor {
private static final long serialVersionUID = 1L;
#Override
public <T> Future<T> submit(Callable<T> task) {
return super.submit(new ContextAwareCallable<T>(task, myContext));
}
This is configurer. Initializes executor. As i understand i can put a TaskDecorator here son i can do logic i need inside that. Still, i need data from method which i cant reach inside TaskDecorator.
#EnableAsync
#Configuration
public class MyAsyncPoolConfig implements AsyncConfigurer {
#Override
#Bean("DefaultAsyncTaskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(0);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(10);
executor.setThreadNamePrefix("myPrefix");
executor.initialize();
return executor;
}
#Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new MyAsyncUncaughtExceptionHandler();
}
}
Async method itself.
#Service("TESTCOMPONENT_ASYNC_SERVICE_METHOD_CALL")
#Async("TESTCOMPONENT_ASYNCTESTER_ASYNC_POOL")
#Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public Future<AsyncCallMethodOutput> asyncMethodCall(AsyncCallMethodInput methodInput) throws MyException {
// actual thing done here
}
This is where async method called.
AsyncServiceExampleService asyncServiceExample = SpringApplicationContext.getContext().getBean(AsyncServiceExampleService.class);
What i need is accessing to AsyncCallMethodInput parameter or better, value of #Service annotation inside ContextAwarePoolExecutor, ContextAwareCallable or a TaskDecorator added to configurer.
This could be done by adding those into context and copying to thread but i need to to this added logic inside Executor or Callable because these are general methods and can serve different async methods. So i don't want to force method writers adding extra data to context which they shouldn't change manually.
Is there a way to achieve this?
I found a working solution. There may be better solutions but thats the only one i can find.
Spring wraps Callable<T> task with another class, which has a property named userDeclaredMethod. When i debugged, this method contains my asyncMethodCall with all metadata i need. So all i need to do access this data.
After even more research i found the following method, that extracts method.
private static Object getField(Object c, String name) throws IllegalAccessException {
while (c != null && !c.getClass().getName().toLowerCase().equals("java.lang.object")) {
try {
Field field = c.getClass().getDeclaredField(name);
field.setAccessible(true);
return field.get(c);
} catch (NoSuchFieldException e) {
c = c.getClass().getSuperclass();
}
}
return null;
}
When i call this method as follows i was able to get what i needed.
Method asyncMethod = (Method) getField(task, "val$userDeclaredMethod");
As i side note, all this code is in ContextAwarePoolExecutor class.

Singleton instance per region

I have a Dynamo DB DAO class, which takes the region like us-east-1, us-east-2 etc., to instantiate the object and interacts with DDB.
Now I am processing a stream of messages, which contains this region value along with other payload to be written to DDB. I want to ensure a single instance of DAO object is created per region.
Currently I have created a map holding all the Dao instances per region and using it for each request to achieve this.
Sample code that I'm using looks like below.
public class DDBDao {
private DynamoDBMapper dynamoDBMapper;
public DDBDao(final string region) {
AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.fromName(region))
.build();
this.dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);
}
public save(..) {
dynamoDBMapper.save(...)
}
....
}
#Singleton
public class DaoContainer {
Map<String, DDBDao> daoContainer = new HashMap<>();
DaoContainer() {
daoContainer.put("us-east-1", new DDBDao("us-east-1"));
daoContainer.put("us-east-2", new DDBDao("us-east-2"));
.....
}
}
I create a instance of DaoContainer and get the DDBDao for the given region to interact with DynamoDB.
What is the best way to create singleton instances of DDBDao per region?
I would suggest implementing custom Region scoped bean, this works exactly same as request/ session scope beans except spring will maintain bean object per Region.
org.springframework.beans.factory.config.Scope is an interface and by implementing it one can create a custom scope in the spring container
public class RegionScope implements Scope {
private final ThreadLocal regionScope = new ThreadLocal() {
protected Object initialValue() {
return new HashMap();
}
};
public Object get(String name, ObjectFactory objectFactory) {
Map scope = (Map) regionScope.get();
Object object = scope.get(name);
if (object == null) {
object = objectFactory.getObject();
scope.put(name, object);
}
return object;
}
public Object remove(String name) {
Map scope = (Map) regionScope.get();
return scope.remove(name);
}
}

Background thread throwing HibernateException - "No Hibernate Session bound to thread..."

I need to create a process that will query a webservice to extract information, and then save the data in my database. However, because this process is very time-intensive, I would like to make it run in the background.
Currently, I have a ProcessHandler which is invoked by a button in the UI. This handler creates a Thread which should run the process in the background. However, I am getting HibernateException with the message No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here.
I have defined ProcessHandler in one of the config xml files (there are several) as follows (this is a very generic definition):
<bean class="com.project.ProcessHandler" parent="parentHandler" />
Inside ProcessHandler, the code to invoke this process is also very generic:
Thread t = new Thread(new WorkerThread(alphaManager, bravoManager, charlieManager));
t.start();
This is the current implementation of WorkerThread:
public class WorkerThread implements Runnable {
private Manager alphaManager;
private Manager bravoManager;
private Manager charlieManager;
public WorkerThread() {
this.alphaManager = null;
this.bravoManager = null;
this.charlieManager= null;
}
public WorkerThread(Manager alphaManager, Manager bravoManager, Manager charlieManager) {
this.alphaManager = alphaManager;
this.bravoManager = bravoManager;
this.charlieManager= charlieManager;
}
#Override
public void run() {
// code to query webservice and extract data...
saveToDbMethod(data);
}
#Transactional(propagation = Propagation.REQUIRED)
private void saveToDbMethod(String data) {
// code to process data...
alphaManager.save(entityA);
bravoManager.save(entityB);
charlieManager.save(entityC);
}
}
The default constructor is a leftover from when I tried to define WorkerThread as a bean in (one of) my config xml files.
Can anyone help me by giving me some tips on how to troubleshoot this?
The problem is that you create the Thread manually and expecting it behave like a spring managed bean.
As the ProcessHandler is a legitimate bean, what i would do is following:
1) Create a seaparate service class which would have the managers as dependencies and that #Transactional method:
#Service
public class Service{
private Manager alphaManager;
private Manager bravoManager;
private Manager charlieManager;
public Service(Manager alphaManager, Manager bravoManager, Manager charlieManager) {
this.alphaManager = alphaManager;
this.bravoManager = bravoManager;
this.charlieManager= charlieManager;
}
#Transactional(propagation = Propagation.REQUIRED)
private void saveToDbMethod(String data) {
// code to process data...
alphaManager.save(entityA);
bravoManager.save(entityB);
charlieManager.save(entityC);
}
}
2) Inject the Service into the ProcessHandler:
<bean class="com.project.ProcessHandler" parent="parentHandler">
<property name="service" ref="service">
</bean>
3) Finally pass the Service to the WorkerThread:
public class WorkerThread implements Runnable {
private Service service;
public WorkerThread(Service service) {
this.service = service;
}
#Override
public void run() {
// code to query webservice and extract data...
service.saveToDbMethod(data);
}
}
and:
Thread t = new Thread(new WorkerThread(service));
t.start();
Now your operations should be transactional and within a session.

How to consume ManagedServiceFactory services

I have a class that implements ManagedServiceFactory like this:
public class GreeterFactory implements ManagedServiceFactory {
private volatile BundleContext bundleContext =
FrameworkUtil.getBundle(GreeterFactory.class).getBundleContext();
private final Map<String, ServiceRegistration> registrations = new ConcurrentHashMap<>();
#Override
public String getName() {
return "Greeter Factory Implementation";
}
/**
* Greeter Service Factory
* #param pid this is the PID of the Configuration received.
* #param dictionary the Configuration to prepare the service.
* #throws ConfigurationException
*/
#Override
public void updated(String pid, Dictionary<String, ?> dictionary) throws ConfigurationException {
String message = (String) dictionary.get("message");
if (message == null) {
throw new ConfigurationException("message",
"Required property 'message' missing");
}
GreeterService greeter;
synchronized (registrations) {
if (registrations.containsKey(pid)) {
greeter = (GreeterService) bundleContext.getService(registrations.get(pid).getReference());
} else {
// For each new configuration, the factory register a new service with
// the given properties/configuration
greeter = new GreeterImpl();
ServiceRegistration greeterRegistration =
bundleContext.registerService(GreeterService.class.getName(),
greeter,
dictionary);
System.out.print("\nRegistering Config-PID: " + pid + "\n");
registrations.put(pid, greeterRegistration);
}
}
greeter.setMessage(message);
}
#Override
public void deleted(String pid) {
ServiceRegistration component = null;
synchronized (registrations) {
component = registrations.remove(pid);
}
// Calling services from a synchronized block can lead to deadlocks,
// so Dependency Manager must be called outside.
if(component != null) {
bundleContext.ungetService(component.getReference());
}
}
}
The factory works OK. I also have a test case to consume the services created for each configuration sent by the ConfigurationAdmin service, here is the test case:
Configuration configuration1 = configurationAdmin.createFactoryConfiguration("example.factoryservice.greeter", null);
Dictionary properties = new Properties();
properties.put("message", "Hello factory world 1!");
configuration1.update(properties);
TimeUnit.SECONDS.sleep(1);
Configuration configuration2 = configurationAdmin.createFactoryConfiguration("example.factoryservice.greeter", null);
properties = new Properties();
properties.put("message", "Hello factory world 2!");
configuration2.update(properties);
TimeUnit.SECONDS.sleep(1);
ServiceReference<GreeterService> sRef = context.getServiceReference(GreeterService.class);
GreeterService greeterService = context.getService(sRef);
assertEquals("Hello factory world 1!", greeterService.sayHello());
greeterService = context.getService(sRef);
assertEquals("Hello factory world 2!", greeterService.sayHello()); // FAILS!!
Now, I am kind of lost here and I cannot find any documentation about this part, but how do I determine in code what Greeter service to use depending on the configuration I need?
I created in code 2 Greeter configurations, the factory then registered a couple of Greeter services, each with a different configuration, how I decide in code an instance of a Greeter service with Configuration 1?
ManagedServiceFactory is pretty low level. Unless you want to implement a technology, you do not need it. In case you want to implement business logic, use one of the Component Models.
But, to answer your specific question:
You register the GreeterService with the service properties that you get from the configuration. That means that you can filter on these services.
Note, that BundleContext has a function where you can pass OSGi service filter as well. E.g.:
Collection<ServiceReference<GreeterService>> sRefs =
context.getServiceReferences(GreeterService.class,
"(message=Hello factory world 2!)");

Categories