Execute some action during server startup in WSO2 Products

Tharika Madurapperuma
2 min readOct 20, 2019

--

There can be a situation where we want to execute specific actions during server startup. This is common to WSO2 Products as well.

In this article I will tell you how to address a specific scenario for WSO2 API Manager.

Scenario :

Copy some files from one directory to another if exists, during server startup.

Here we simply need a way to do something when the server starts. There are 2 ways to do this, depending on your requirement.

1. Externally plugin some code to do the task.

This method is more common among the users of the product. And it can be done by following this documentation.

2. Internally do it within the product code.

For developers, this method will be useful and is very similar to the first method above. And I will explain how this can be done.

We need 2 classes for this purpose.

  1. A Startup Listener — The class that contains the logic for the task we need to perform.
  2. A Service Component — The class that will register our listener as an OSGI service.

Note that these classes should be newly created if you do not already have a common class for tasks that should be done during server startup. Before implementing it is good to always check whether you already have a Startup Listener.

First let’s write our Startup Listener class.

This class implements ServerStartupObserver in carbon core component of WSO2 Products.

ServerStartupListener.java

ServerStartupListener.java

Next we should write the service component class which would help us register the new Listener as an OSGI service which will run during server startup.

StartupServiceComponent.java

StartupServiceComponent.java

You can now build the component and put the jar as a patch and detect whether your task is successfully executed.

Now you have successfully completed writing a ServerStartupListener to copy a file from one directory to another if it exists.

Thank you for reading the article!

If you find any outdated content or issues with this article, please feel free to create an issue at Developer Corner Git repository here. Let’s grow together and help others in their journey too!

If you like this article please give it a clap. 🙂

Cheers!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response