How to remove timers from EJB container for Singleton beans on undeploy? - singleton

I have the following #Singleton bean that I am using to perform some scheduled tasks:
#Singleton
#Startup
public class SqsScheduler {
// Logger-------------------------------------------------------------------
private static final Logger LOG = Logger.getLogger(SqsScheduler.class.getName());
// Variables----------------------------------------------------------------
Timer timer;
StoredDynamoQueries storedDynamoQueries = new StoredDynamoQueries();
// Constructors-------------------------------------------------------------
public SqsScheduler() {
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduledTask(), 0, 180 * 1000);
}
// Methods------------------------------------------------------------------
class ScheduledTask extends TimerTask {
#Override
public void run() {
// The scheduled tasks to perform
}
}
}
Everything works fine EXCEPT when I undeploy/redeploy the the application the TimerTasks don't get removed and the redeployed application then starts producing errors. If I undeploy the application, restart the server (Glassfish 3.1.2.2) and then deploy the application from scratch it works perfectly.
How would I go about removing the timers when the application is redeployed?

As per perissf comment:
With EJBs it's recommended to use the Java EE Timer Service

Related

Spring Integration testing a Files.inboundAdapter flow

I have this flow that I am trying to test but nothing works as expected. The flow itself works well but testing seems a bit tricky.
This is my flow:
#Configuration
#RequiredArgsConstructor
public class FileInboundFlow {
private final ThreadPoolTaskExecutor threadPoolTaskExecutor;
private String filePath;
#Bean
public IntegrationFlow fileReaderFlow() {
return IntegrationFlows.from(Files.inboundAdapter(new File(this.filePath))
.filterFunction(...)
.preventDuplicates(false),
endpointConfigurer -> endpointConfigurer.poller(
Pollers.fixedDelay(500)
.taskExecutor(this.threadPoolTaskExecutor)
.maxMessagesPerPoll(15)))
.transform(new UnZipTransformer())
.enrichHeaders(this::headersEnricher)
.transform(Message.class, this::modifyMessagePayload)
.route(Map.class, this::channelsRouter)
.get();
}
private String channelsRouter(Map<String, File> payload) {
boolean isZip = payload.values()
.stream()
.anyMatch(file -> isZipFile(file));
return isZip ? ZIP_CHANNEL : XML_CHANNEL; // ZIP_CHANNEL and XML_CHANNEL are PublishSubscribeChannel
}
#Bean
public SubscribableChannel xmlChannel() {
var channel = new PublishSubscribeChannel(this.threadPoolTaskExecutor);
channel.setBeanName(XML_CHANNEL);
return channel;
}
#Bean
public SubscribableChannel zipChannel() {
var channel = new PublishSubscribeChannel(this.threadPoolTaskExecutor);
channel.setBeanName(ZIP_CHANNEL);
return channel;
}
//There is a #ServiceActivator on each channel
#ServiceActivator(inputChannel = XML_CHANNEL)
public void handleXml(Message<Map<String, File>> message) {
...
}
#ServiceActivator(inputChannel = ZIP_CHANNEL)
public void handleZip(Message<Map<String, File>> message) {
...
}
//Plus an #Transformer on the XML_CHANNEL
#Transformer(inputChannel = XML_CHANNEL, outputChannel = BUS_CHANNEL)
private List<BusData> xmlFileToIngestionMessagePayload(Map<String, File> xmlFilesByName) {
return xmlFilesByName.values()
.stream()
.map(...)
.collect(Collectors.toList());
}
}
I would like to test multiple cases, the first one is checking the message payload published on each channel after the end of fileReaderFlow.
So I defined this test classe:
#SpringBootTest
#SpringIntegrationTest
#ExtendWith(SpringExtension.class)
class FileInboundFlowTest {
#Autowired
private MockIntegrationContext mockIntegrationContext;
#TempDir
static Path localWorkDir;
#BeforeEach
void setUp() {
copyFileToTheFlowDir(); // here I copy a file to trigger the flow
}
#Test
void checkXmlChannelPayloadTest() throws InterruptedException {
Thread.sleep(1000); //waiting for the flow execution
PublishSubscribeChannel xmlChannel = this.getBean(XML_CHANNEL, PublishSubscribeChannel.class); // I extract the channel to listen to the message sent to it.
xmlChannel.subscribe(message -> {
assertThat(message.getPayload()).isInstanceOf(Map.class); // This is never executed
});
}
}
As expected that test does not work because the assertThat(message.getPayload()).isInstanceOf(Map.class); is never executed.
After reading the documentation I didn't find any hint to help me solved that issue. Any help would be appreciated! Thanks a lot
First of all that channel.setBeanName(XML_CHANNEL); does not effect the target bean. You do this on the bean creation phase and dependency injection container knows nothing about this setting: it just does not consult with it. If you really would like to dictate an XML_CHANNEL for bean name, you'd better look into the #Bean(name) attribute.
The problem in the test that you are missing the fact of async logic of the flow. That Files.inboundAdapter() works if fully different thread and emits messages outside of your test method. So, even if you could subscribe to the channel in time, before any message is emitted to it, that doesn't mean your test will work correctly: the assertThat() will be performed on a different thread. Therefore no real JUnit report for your test method context.
So, what I'd suggest to do is:
Have Files.inboundAdapter() stopped in the beginning of the test before any setup you'd like to do in the test. Or at least don't place files into that filePath, so the channel adapter doesn't emit messages.
Take the channel from the application context and if you wish subscribe or use a ChannelInterceptor.
Have an async barrier, e.g. CountDownLatch to pass to that subscriber.
Start the channel adapter or put file into the dir for scanning.
Wait for the async barrier before verifying some value or state.

Is it possible to add completion items to a Microsoft Language Server in runtime?

I am trying to develop a IntelliJ plugin which provides a Language Server with help of lsp4intellij by ballerina.
Thing is, i've got a special condition: The list of completion items should be editable in runtime.
But I've not found any way to communicate new completionItems to the LanguageServer process once its running.
My current idea is to add an action to the plugin which builds a new jar and then restarts the server with the new jar, using the Java Compiler API.
The problem with that is, i need to get the source code from the plugin project including the gradle dependencies accessable from the running plugin... any ideas?
If your requirement is to modify the completion items (coming from the language server) before displaying them in the IntelliJ UI, you can do that by implementing the LSP4IntelliJ's
LSPExtensionManager in your plugin.
Currently, we do not have proper documentation for the LSP4IntelliJ's extension points but you can refer to our Ballerina IntelliJ plugin as a reference implementation, where it has implemented Ballerina LSP Extension manager to override/modify completion items at the client runtime in here.
For those who might stumble upon this - it is indeed possible to change the amount of CompletionItems the LanguageServer can provide during runtime.
I simply edited the TextDocumentService.java (the library I used is LSP4J).
It works like this:
The main function of the LanguageServer needs to be started with an additional argument, which is the path to the config file in which you define the CompletionItems.
Being called from LSP4IntelliJ it would look like this:
String[] command = new String[]{"java", "-jar",
"path\\to\\LangServer.jar", "path\\to\\config.json"};
IntellijLanguageClient.addServerDefinition(new RawCommandServerDefinition("md,java", command));
The path String will then be passed through to the Constructor of your CustomTextDocumentServer.java, which will parse the config.json in a new Timer thread.
An Example:
public class CustomTextDocumentService implements TextDocumentService {
private List<CompletionItem> providedItems;
private String pathToConfig;
public CustomTextDocumentService(String pathToConfig) {
this.pathToConfig = pathToConfig;
Timer timer = new Timer();
timer.schedule(new ReloadCompletionItemsTask(), 0, 10000);
loadCompletionItems();
}
#Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(CompletionParams completionParams) {
return CompletableFuture.supplyAsync(() -> {
List<CompletionItem> completionItems;
completionItems = this.providedItems;
// Return the list of completion items.
return Either.forLeft(completionItems);
});
}
#Override
public void didOpen(DidOpenTextDocumentParams didOpenTextDocumentParams) {
}
#Override
public void didChange(DidChangeTextDocumentParams didChangeTextDocumentParams) {
}
#Override
public void didClose(DidCloseTextDocumentParams didCloseTextDocumentParams) {
}
#Override
public void didSave(DidSaveTextDocumentParams didSaveTextDocumentParams) {
}
private void loadCompletionItems() {
providedItems = new ArrayList<>();
CustomParser = new CustomParser(pathToConfig);
ArrayList<String> variables = customParser.getTheParsedItems();
for(String variable : variables) {
String itemTxt = "$" + variable + "$";
CompletionItem completionItem = new CompletionItem();
completionItem.setInsertText(itemTxt);
completionItem.setLabel(itemTxt);
completionItem.setKind(CompletionItemKind.Snippet);
completionItem.setDetail("CompletionItem");
providedItems.add(completionItem);
}
}
class ReloadCompletionItemsTask extends TimerTask {
#Override
public void run() {
loadCompletionItems();
}
}
}

How to schedule a task on Hazelcast that queries on the IMap?

I want to schedule a task on Hazelcast that runs at a fixed interval and updates the IMap with some data that I get after hitting a rest endpoint. Below is a sample code:
// Main class
IScheduledExecutorService service = hazelcast.getScheduledExecutorService("default");
service.scheduleAtFixedRate(TaskUtils.named("my-task", myTask), 30, 1);
// Task
#Singleton
public class MyTask implements Runnable, Serializable {
RestClient restClient;
IMap<String, JsonObject> map;
#Inject
MyTask() { // Inject hazelcast and restclient
map = hazelcastInstace.getMap("my-map");
this.restClient = restClient;
}
#Override
public void run() {
Collection<JSONObject> values = map.values(new MyCustomFilter());
for(JSONObject obj : values) {
// query endpoint based on id
map.submitToKey(key, response);
}
}
private static class MyCustomFilter implements Predicate<String, JSONObject> {
public boolean apply(Map.Entry<String, JSONObject> map) {
// logic to filter relevant keys
}
}
}
When I try to execute this on the cluster, I get:
java.io.NotSerializableException: com.hazelcast.map.impl.proxy.MapProxyImpl
Now I need the IMap to selectively query only some keys based on PredicateFilter and this needs to be a background scheduled job so stuck here on how to take this forward. Any help appreciated. TIA
Try making your task also implement HazelcastInstanceAware
When you submit your task, it is serialized, sent to the grid to run, deserialized when it is received, and the run() method is called.
If your task implements HazelcastInstanceAware, then between deserialization and run(), Hazelcast will call the method setHazelcastInstance(HazelcastInstance instance) to pass your code a reference to the particular Hazelcast instance it is running in. From there you can just do instance.getMap("my-map") and store the map reference in a transient field that the run() method can use.

Suspend MDB message processing until #StartupBean has finished initialization

While migrating a JBoss 5 application to JBoss AS 7 (7.1.1.FINAL) I have a problem with a new JMS message driven EJB. Within message processing, some master data fields have to be checked. To enhance performance, this master data shall be preloaded into a cache structure using a #Singleton #Startup EJB, which needs about 30 seconds to load the data.
My problem is that the queue message processing starts even if the cache has not been fully initialized, causing message validation errors.
I tried to define a dependency between the MDB and the startup EJB, but as far as I understood the #DependsOn annotation works only with #Singleton EJBs. So it's clear that my solution does not work ;-)
Startup bean code:
#Singleton
#Startup
public class StartupBean {
#PostConstruct
void atStartup() {
// TODO load master data cache (takes about 30 seconds)
}
#PreDestroy()
void atShutdown() {
// TODO free master data cache
}
}
Note: I stripped the real code from the example to make it easier to read :-)
Message driven bean code:
#MessageDriven(name="SampleMessagingBean", activationConfig = {
#ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
#ActivationConfigProperty(propertyName="destination", propertyValue="jms/SampleQueue"),
#ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
#DependsOn("StartupBean")
public class SampleMessagingBean implements MessageListener {
public void onMessage(Message message) {
// TODO validate message using master data cache
}
}
Question: How can I suspend message processing until the startup bean has finished loading the cache?
Any suggestions greatly appreciated :-)!
First i thought inject singleton EJB in mdb would be enough to delay message consumption
But no, sometimes it would start consuming the message before #PostConstruct of Singleton-ejb completed. So added a method invocation also and it started working
This worked on glassfish, but i dont see a reason why it shouldnt work on jboss
Singleton-Ejb:
#Singleton
#Startup
public class SingletonBean {
private Logger logger = Logger.getLogger(getClass().getName());
private boolean init = false;
public boolean isInit() {
return init;
}
#PostConstruct
public void init() {
logger.error("singleton init start");
//Do something that takes time here
init = true;
logger.error("singleton init end ");
}
}
and mdb:
#MessageDriven(...)
public class SomeMdb implements MessageListener {
private Logger logger = Logger.getLogger(getClass().getName());
#EJB
SingletonBean sb;
#PostConstruct
public void init() {
logger.error("mdb init start");
if (!sb.isInit()) {
logger.error("never happens");
}
logger.error("mdb init complete");
}
public void onMessage(Message message) {
logger.error("onMessage start");
}
}
Now it always waits for SingletonBean to complete init before mdb completes init (as seen in log)
19:51:51,980 [ad-pool-1; w: 3] ERROR SomeMdb - mdb init start
19:51:52,122 [ad-pool-4848(4)] ERROR SingletonBean - singleton init start
19:51:56,316 [ad-pool-4848(4)] ERROR SingletonBean - singleton init end
19:51:56,317 [ad-pool-1; w: 3] ERROR SomeMdb - mdb init complete
19:51:56,317 [ad-pool-1; w: 3] ERROR SomeMdb - onMessage start

New MVC 4 Beta Web API is not serving requests when ran as a Windows Service

I'm trying to run a self hosted executable as a Windows service. I'm using the MVC 4 beta Web API. First I used Derik Whittaker's blog for setting up the basic console application and tested it with positive results.
I then used Einar Egilsson's blog to make it work as both a console application and a windows service. The application installed as a service just fine. I set the service logon to use my own for this basic testing; it failed to bind to the socket without this. When the service starts up I see all my trace logs as expected there are no fatal errors. The application appears to be running normally. When I test using fiddler using the same request for the console application I get a "HTTP/1.1 500 Internal Server Error".
Using this same code when I turn off the service then launch using F5 in VS the application starts up just fine and serves the same request!? The log entries are identical within the same execution paths.
public partial class TestService : ServiceBase {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private HttpSelfHostServer _server;
static void Main(string[] args) {
Logger.Debug("Main Called");
var service = new TestService();
if (Environment.UserInteractive) {
Logger.Debug("Environment.UserInteractive == true");
Console.WriteLine("Press any key to stop program");
service.OnStart(args);
service.OnStop();
} else {
Logger.Debug("Environment.UserInteractive == false");
try {
Run(service);
} catch(Exception exception) {
Logger.Fatal(exception.Message, exception);
}
}
}
protected override void OnStart(string[] args) {
Logger.Debug("OnStart called");
var hostUri = string.Format("http://{0}:{1}", Environment.MachineName, ConfigurationManager.AppSettings["Service.Port"]);
Logger.Debug("URL:" + hostUri);
var selfHostConfiguration = new HttpSelfHostConfiguration(hostUri);
selfHostConfiguration.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "endpoints/{controller}",
defaults: null
);
Logger.Debug("Routes registered");
try {
using (_server = new HttpSelfHostServer(selfHostConfiguration)) {
Logger.Debug("Hosting at " + hostUri + "/endpoints/{controller}");
_server.OpenAsync().Wait();
if (Environment.UserInteractive) { // *** I've tried this commented out as well
Console.ReadLine();
}
Logger.Debug("End of using");
}
} catch(Exception exception) {
Logger.Fatal(exception.Message, exception);
if(exception.InnerException != null) {
Logger.Fatal(exception.InnerException.Message, exception.InnerException);
}
}
}
protected override void OnStop() {
_server.Dispose();
}
}
It has been some time since you posted this but I wanted to input.
I don't know why this is, but the internal error comes from initializing the self host inside the onStart method. You must initialize it in the constructor of the service and then only call the _server.OpenAsync() in the onStart method.
Or at least that is what worked for me.
Use TopShelf. I just blogged about how to do that.
as i can see, you use _server.OpenAsync().Wait(); in OnStart method. This just makes your initialization code to freeze, all code after that line will not execute. To avoid this try to remove .Wait() from the OpenAsync().