How do I run a method before republishing to JBoss? - eclipse

I'm developing a J2EE web application and I would like to be able to run a method (or function, class, whatever - something) during the "republish" process. It would be nice if I could control when during the republish my function gets called (before, during, after, etc.) but a good first step would be getting something to be called automatically.
As a temporary hack, I was able to add a button to my web app that you click right before you click "republish" in Eclipse.

Implement ServletContextListener to hook on webapp's startup and shutdown.
public class Config implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// Do stuff during startup.
}
public void contextDestroyed(ServletContextEvent event) {
// Do stuff during shutdown.
}
}
To get it to work, just register it in web.xml.
<listener>
<listener-class>com.example.Config</listener-class>
</listener>
I am however only not sure what exactly you mean with during publish. But you could take a look for another listeners available in the Servlet API or maybe a Filter.

Related

asp.net core starts up ok, but all Http requests fail to run

Recently I started a new Asp.Net core application 1.0, and copied some existing code across from another project. After adding a considerable amount of code, and getting it to compile, I found that starting the application, appeared to start up, but when it tried to hit the Home controller to get the start page of the application, the browser reported it could NOT reach the web page. Reported a This site can’t be reached message (as if the web application was not running). This was very frustrating as the app used to work fine before I added all this extra code.
I ended up having to go back and start a brand new application (in the same project), and add components back into the project one at a time, till i found the thing that had caused this issue. A frustrating day passed where I could not get any errors from the server at all regarding why it failed to find the web page.
After a lot of menial work and cursing, I eventually tracked down the issue.
The issue was that I had added some support to do the database seeding as part of the Configure method of the Startup class. I had made this a Async Static Method, and called the method using an await inside the Configure method. Naturally I had to make the configure method Async which I did.
The reasons for adding these awaits, were so I could use Async calls within my DatabaseSeed method call
public class Startup
{
...
public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
await DbExtensions.EnsureSeedData(app.ApplicationServices);
}
...
}
}
public static class DbExtensions
{
public static async Task EnsureSeedData(IServiceProvider serviceProvider)
{
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
var auditService = serviceScope.ServiceProvider.GetService<AuditService>();
....
}
...
}
WARNING: DO NOT use the code above as an example of what to do.
So this all compiled fine and the application started up as per normal. But like i stated above, No Http calls would find the web application
So I found out that due to the await call in the configure method (which was also marked as async now), this caused the application to be in a ghost state, and not responding to any routing calls.
My question to anyone that can answer, is why the routed calls didn't register anywhere as an issue, when it failed to access the Web application?
Note I couldn't even get a debug breakpoint to be hit in any controller code.

Strategies for implementing a user specific - custom content EntryPoint in GWT

We have developed a dashboard in GWT that contains some custom widgets for displaying customer's data in various graphical forms. We now want to move to a more custom / user specific approach where each customer who logs into the dashboard can see a different perspective of the dashboard. Some widgets will be available for some users, for some others not and with different initialization parameters.
We are trying to find an efficient strategy to do this. A potential solution would be to have the client side request all this information during EntryPoint loading and then use that incoming configuration to build itself and make further requests for the data. A more efficient solution would also allow downloading to the browser only those widgets relevant to the user.
Does GWT have any design pattern for this scenario? If not, what would a good high level solution be for this case?
Thank you.
Yes there is atleast one mechanism that can be useful to achieve your requirements
Code Splitting :- The idea is to split the particular section of code and download it in independent async call. Using this approach you can reduce the size of primary javascript file making initial page load faster. This approach allow GWT to skip inclusion of all those widgets which have references only inside GWT.runAsync() method in primary js file. Such widgets and code gets downloaded when application runs that code in independent call. you can use this approach and avoid download of additional dashboard charts based on some conditions like user type and so. Here is sample code from GWT reference website
public class Hello implements EntryPoint {
public void onModuleLoad() {
Button b = new Button("Click me", new ClickHandler() {
public void onClick(ClickEvent event) {
GWT.runAsync(new RunAsyncCallback() {
public void onFailure(Throwable caught) {
Window.alert("Code download failed");
}
public void onSuccess() {
Window.alert("Hello, AJAX");
}
});
}
});
RootPanel.get().add(b);
}
}

AEM listen to bundle registration

I know I can use a JCR EventListener to check things like changes on nodes. I also know OSGi bundles implement a ServiceListener interface which let them know when a bundle is registered or stopped.
I think I'm somewhat close but I can't seem to connect the dots. How in AEM can I deploy a bundle that can listen to other bundles ServiceEvent changes?
Yes, you are right. You can listen to OSGi events, using Felix EventAdmin.
There you can find specification but in a couple of words:
First you need to implement interface EventHandler
Register your handler for events with topic org/osgi/framework/BundleEvent/STARTED
In documentation they do not use annotations, but if you use maven scr plugin in you project - you can do it with annotations. Your code could look like:
#Component
#Service(value = EventHandler.class)
#Property(name = EventConstants.EVENT_TOPIC, value = { ReplicationAction.EVENT_TOPIC })
public class YourEventHandler implements EventHandler {
#Override
public void handleEvent(Event event) {
// do smth with event
}
}
What is your use case? do you need a BundleTracker or a ServiceTracker.
As may be the case you may need to extend BundleTracker/ServiceTracker and handle your logic in addingxxxxxx/ modifiedxxxxxx/ removedxxxxxx methods.
Some examples you can check AEM commons code and Tracker

How to include 3rd party JavaScript libraries in a reusable gwt library/widget?

I'm trying to get my feet wet with GWT to see if migrating will work out. I usually try the more difficult parts first to make sure I can finish the project. The most difficult part of my project(s) is referencing 3rd party JS libs. In this example I'm trying to use PubNub as much of our platform uses it.
What I'd like to do is create a reusable object that can be used in other GWT projects in need of PubNub. I've got a simple little test running successfully (ie, I've got the basics of JNSI working), but my question is -> where do I put the reference to the 3rd party script in order to create the library/module properly?
Right now I just put the reference to the external scripts in the HTML page in the project, but I'm pretty sure this is incorrect from a reusability perspective, as this lib would be used in other projects, each of which would have their own base HTML page.
I tried putting the reference in the gwt.xml file, but this seems to lose the references (ie my test project no longer works as it did when the scripts were in the HTML page)
Do you have any tips on how to include 3rd party libraries in a reusable GWT library/widget?
Here you have an example using client bundles and script injector, you can use either synchronous loading or asynchronous.
When using sync the external js content will be embedded in the application, otherwise it will be include in a different fragment which will be got with an ajax request.
You can put your api in any server and load it with the ScriptInjector.
public class Example {
public static interface MyApiJs extends ClientBundle {
MyApiJs INSTANCE = GWT.create(MyApiJs.class);
#Source("my_api.js")
TextResource sync();
#Source("my_api.js") // Should be in the same domain or configure CORS
ExternalTextResource async();
}
public void loadSync() {
String js = MyApiJs.INSTANCE.sync().getText();
ScriptInjector.fromString(js).inject();
}
public void loadAsync() throws ResourceException {
MyApiJs.INSTANCE.async().getText(new ResourceCallback<TextResource>() {
public void onSuccess(TextResource r) {
String js = r.getText();
ScriptInjector.fromString(js).inject();
}
public void onError(ResourceException e) {
}
});
}
public void loadFromExternalUrl() {
ScriptInjector.fromUrl("http://.../my_api.js").inject();
}
}
[EDITED]
A better approach is to use a new feature in gwtquery 1.4.0 named JsniBundle. We introduced this feature during the GWT.create conferences at San Francisco and Frankfurt.
With this approach you can insert any external javascript (placed in your source tree or hosted in an external host) as a JSNI block. It has many benefits:
Take advantage of GWT jsni validators, obfuscators and optimizers.
Get rid of any jsni java method when the application does not use it.
The syntax is actually easy:
public interface JQueryBundle extends JsniBundle {
#LibrarySource("http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js")
public void initJQuery();
}
JQueryBundle jQuery = GWT.create(JQueryBundle.class);
jQuery.initJQuery();

Create a GWT RPC Service

I’m trying to create a backend for a homepage with GWT. I created a Google Web Application in Eclipse without sample code and now I would like to add the service, but the developer Google guide doesn’t help me. I’m not sure, where to add the interface and how it exactly works.
If I understand the google documentation correctly, I have to add a module and an entry point class, is that correct? It would be great if you could give me some tips and help how to create a rpc service.
If you create a new GWT project in the Eclipse "New Project" wizard, with "Generate project sample code" checked, it will include a fully functioning RPC service with a sample method, which you can then adapt or copy according to your needs.
Out of memory, don't have eclipse in front of me.
First do create a test project with generated testcode, you can delete it afterward.
Yes you will have to add a module.
Create in client the two interfaces for the async calls, inherit it on server side.
Hope I understood your question right.
I'm not sure what would help you the most. Google developer guide was enough for me (at least when I started using it on version 1.6) to create RPC services for my GWT application.
General APP
Module: is the .gwt.xml file. Yes, you'll need it. The GWT compiler will find it automagically and try to compile all the GWT code (the <source> element will tell which subpackage contains Java code that will be converted to JS). It will tell also which class implements the EntryPoint interface. The onModuleLoad will be the code executed when javascript runs in the client page.
RPC
Well, you should first try UI things and only then, when you're confident enough, try the server thing. Anyway the scheme is:
interface MyService extends RemoteService {
List<String> doSomething(String sample, int other);
}
#RemoteServiceRelativePath("../path/to/servlet") // see later
intercace MyServiceAsync {
void doSomething(String sample, int other, AsyncCallback<List<String>> callback);
}
These are the interfaces. Later is the async one. That's what you'll use from client side. Always calling and passing an implementation of AsyncCallback which will receive (sometime later, you don't know when) the result.
First interface is the syncrhonous one. That is what you need to implement on server. You must inherit from RemoteServiceServlet class (it is an implementation of servlet that already does all the values handling), and implement your interface. GWT code does the rest (almost).
public class ServiceImpl extends RemoteServiceServlet implements MyService
{
// implement the method normally
}
From client you'll need to create the service proxy:
private static MyServiceAsync MY_SERVICE = GWT.create(MyService.class);
Yes. I know it's weird how GWT knows MyserviceAsync and MyService work together. Don't worry about that. It works :)
Just use the service like this:
MY_SERVICE.doSomething("value", 111, new AsyncCallback<List<String>>() {
// note that this code executes some time in the future when response from server is back
public void onSuccess(List<String> result) {
Window.alert("Server answered with " + result.size() + " elements!");
}
public void onFailure(Throwable t) {
Window.alert("Server failed: " + t.getMessage());
}
}
Path to server
You'll have to configure your app to make that servlet implementation listen to URL indicated in #RemoteServiceRelativePath. That's the way client knows where to make the request, and the server knows which servlet attends that request. I'd suggest using:
../my-service.gwt as relative path (GWT module gets published in <ROOT>/module_name
and
configuring your web app to use the servlet for /my-service.gwt
But it's entirely upon your preferences :)
Anyway I think Google tutorials are the best. So please copy&paste. Try&modify until you get to understand the whole thing.