I would like to execute a code once an application gets deployed completely on JBOSS, is there a way to intercept application deployment on JBOSS, or is there a point where I can be very sure that the application has been deployed completely and I can execute my code just after that point.
Reading Execute code after Glassfish Web Deployment i came to the answer of this question.
We have the ability to code a ServletContextListener to be triggered when the context is loaded like this:
public class MyServlet implements ServletContextListener {
public void contextInitialized(ServletContextEvent e) {
// implementation code
}
public void contextDestroyed(ServletContextEvent e) {
// implementation code
}
}
Reference:
Example of ServletContextListener
Thanks to Garis Suero
Related
I have a basic SpringMVC Application which is running (and mapping) fine.
Now I wanted to set up my UnitTests with MockMvc to perform get requests and stuff.
But if I run the test there is an AssertionError "Status expected: <200> but was: <404>" and my console gives warning "No mapping found for HTTP request with URI [/] in DispatcherServlet with name ''".
I get the feeling my MockMvc cant communicate with my DispatcherServlet, so how do I define this connection?
Here is my short test class:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextConfiguration
public class HomeControllerTest {
private MockMvc mvc;
#Autowired
private WebApplicationContext wac;
#Before
public void setup() throws ServletException {
mvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
#Test
public void testLandingPagePath() throws Exception {
mvc.perform(get("/")).andExpect(status().isOk());
}
}
So I expected MockMvc to get the location of my DispatcherServlet by default. But it actually doesn't call it for mapping.
I have my "web.xml" and "dispatcher-servlet.xml" located in "WEB-INF" folder and no extra configuration defined.
I got the feeling the problem is based on my project structure, but this is a basic eclipse "Dynamic Web Application", the tests are located in "src/test/java", parallel to "src/main/java".
I appreciate any help, since I spend last 2 hours on reading for solutions but not getting the trick.
After some more time I got things right now:
There is still no way for me to access the dispatcher-servlet.xml in WEB-INF folder. I added a new "config" package to my test-package in parallel to my "controller" test package and put a copy of the dispatcher-servlet.xml in there, called "test-dispatcher-servlet.xml".
Now my test class is able to access this with
#ContextConfiguration("../config/test-dispatcher-servlet.xml")
public class HomeControllerTest {
Hope this can help others who start with spring mvc testing :-)
I have setup a SpringBootAdmin server, and trying to register an application with SpringBootAdmin client. It does not seem to be registering.
Do I neccesarily have to register with Eureka?
How do I debug?
Configuration on adminserver
build.gradle
dependencies {
compile('de.codecentric:spring-boot-admin-server-ui')
compile('de.codecentric:spring-boot-admin-server-ui-login')
compile('de.codecentric:spring-boot-admin-server')
compile('org.springframework.boot:spring-boot-starter-web-services')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
application.properties
spring.application.name=Boot-Admin
server.port=8093
security.user.name=admin
security.user.password=admin
logging.level.de.codecentric.boot.admin.client=DEBUG
logging.level.de.codecentric.boot.admin=DEBUG
App is
#SpringBootApplication
#Configuration
#EnableAdminServer
public class AdminApp {
public static void main(String[] args) {
SpringApplication.run(AdminApp.class, args);
}
}
On the client side,
build.gradle
dependencies {
compile('de.codecentric:spring-boot-admin-starter-client')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web-services')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
application.properties
server.port=8091
spring.boot.admin.client.enabled=true
spring.boot.admin.url=http://localhost:8093
spring.boot.admin.client.auto-registration=true
spring.boot.admin.username=admin
spring.boot.admin.password=admin
logging.level.de.codecentric.boot.admin.client=DEBUG
Code is
#Configuration
#SpringBootApplication
public class SBACApp {
public static void main(String[] args) {
SpringApplication.run(SBACApp.class, args);
}
}
According to all the Stackoverflow articles and tutorials, this should be adequate.
Even though logging is set on the client side, there seems to be no log-line starting with d.c....
What could I be missing
Any additional knowledge on how to debug this may help.
If you are using spring boot admin 2.0 the the client url property would be
spring.boot.admin.client.url: http://localhost:8093
I would check to see what version you are using and then double check the property names.
In Spring Boot 2.x.x version client url property is different that Spring Boot 1.x.x
spring.boot.admin.client.enabled=true
spring.boot.admin.client.url= http://localhost:8093
spring.boot.admin.client.auto-registration=true
make sure you have other two properties. in my case i was missing auto registration property
I've got that JAX-RS webservice:
#Path("/my")
public class MyWS {
#Inject
private MyService myService;
#POST
#Path("/save")
#Consumes("application/json")
public void save(SaveParam saveParam) {
myService.save(saveParam);
}
}
and that #Stateless service:
#Stateless
public class MyService {
#PersistenceContext
protected EntityManager entityManager;
#TransactionAttribute
public void save(SaveParam saveParam) {
entityManager.persist(saveParam);
}
}
Using JBoss EAP 6.2, myService is always null (not injected) when arriving in the save method.
I've tried to add #ApplicationScoped to MyWS class, but the same behavior is happening (NullPointerException). The only solution is to declare MyWS also as #Stateless, but that's not really have any sens, have it?
Is this a bug in JBoss EAP?
All my classes are in the same war project and I have a WEB-INF/beans.xml using CDI 1.0 spec. I also have a class extending javax.ws.rs.core.Application and declaring the #ApplicationPath.
I guess it is a bug if you configure your REST service in web.xml.
I have the same issue and i was getting crazy about this.
Just clean up the web.xml and it should not have anything related to REST.
#ApplicationPath is good enough for all REST setup.
Where do I need to place jetty.xml, in case of embedded Jetty?
jetty-web is placed inside WEB-INF and it is loaded automatically. I tried to put there jetty.xml, but it failed to load (it does not see it). I am using mvn jetty:run to run the service.
Their WIKI example doesn't work? http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
public class FileServerXml {
public static void main(String[] args) throws Exception {
Resource fileserver_xml = Resource.newSystemResource("fileserver.xml");
XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
Server server = (Server)configuration.configure();
server.start();
server.join();
}
}
It sounds like you need to restructure your jetty.xml in any event.
I've followed the instructions here to create a client to a remote SessionBean. I run the client on the same machine that Glassfish 3.1.2 beta is running on. When I use the gf-client.jar from the 3.1.2 beta Glassfish I get the following Exception which is the same Exception if I leave the gf-client.jar out of the classpath:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
If I use a 3.1.1 gf-client.jar from a Maven repository I get a huge stack trace with complaints about it not being able to find some .jar files from Derby which I'm not even using. Apparently a version mismatch problem.
Has anyone gotten a standalone client to connect to Glassfish 3.1.2 beta? Did this change in JavaEE 6?
Here's the code:
#Stateless
public class LockTestDeadlockService implements LockTestDeadlockServiceI {
public int getP1Id() throws SQLException {
int parentId = -1;
return parentId;
}
}
#Remote
public interface LockTestDeadlockServiceI {
public int getP1Id() throws SQLException;
public void insertChildUpdateParent(int parentId) throws SQLException;
}
Here's my client:
public class LoadTestClient {
static Logger logger = Logger.getLogger(LoadTestClient.class);
public static void main(String[] args) {
String jndiName = "java:global/locktest-0.0.1-SNAPSHOT/LockTestDeadlockService";
try {
LockTestDeadlockServiceI lockTestService =
(LockTestDeadlockServiceI) new InitialContext().lookup(jndiName);
logger.info("Got lockTestService Remote Interface");
} catch (NamingException e) {
logger.info("Failed to get lockTestService Remote Interface: " + e);
}
}
}
The short answer is that to connect to GF 3.x from a client, you need a mini-glassfish install via the Application Client Container (ACC) using either webstart or the package-appclient script. Open up the gf-client.jar and look at its classpath in the manifest file. There are a ton of files listed in there. This was similar in GF 2.x, but it seemed to need less dependencies on the client (though it was 15MB with that version).
See these:
Create an "Application Client" with Maven in Java EE
With which maven dependencies can i create a standalone JMS client for Glassfish?
http://docs.oracle.com/cd/E18930_01/html/821-2418/beakt.html#scrolltoc
http://docs.oracle.com/cd/E18930_01/html/821-2418/beakv.html#beakz