How to initialize SpringContext once and share across tasks?

307 Views Asked by At

I am trying to initialize spring context in my Spark application. I want the context in my slave nodes as well as I want to re-use the beans. Here is the code for the same:-

shipperRD2.foreach(shipper->{

 AmazonS3 amazonS3Client = AmazonS3ClientBuilder.standard().build();
                    FileSystemXmlApplicationContext context2 = new FileSystemXmlApplicationContext("https://s3.console.aws.amazon.com/s3/object/spring-configuration/app-context.xml");

PersistenceWrapper persistenceWrapper = context.getBean(PersistenceWrapper.class);
});

However, this is leading to context refresh every time a new task runs on the slave node. Is there any way to avoid this behavior. basically, just initialize the context on the first task run, and re-use that context in the subsequent tasks.

1

There are 1 best solutions below

0
On BEST ANSWER

As mentioned by Jacek, I tried the singleton pattern and it worked.

public class SpringInit {

    private static FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(fileName);

    private SpringInit(){
    }

    public static FileSystemXmlApplicationContext getInstance(){
        return context;
    }
 }

From the spark,

shipperRD2.foreach(shipper->{

  FileSystemXmlApplicationContext context = SpringInit.getInstance();
PersistenceWrapper persistenceWrapper = context.getBean(PersistenceWrapper.class);
});