/api.jar: EchoService Secret
Last active
August 29, 2015 14:01
Star
You must be signed in to star a gist
Tomcat JMX Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.service; | |
public interface EchoService { | |
public static final String MBEAN_NAME = "com.example:type=service,name=EchoService"; | |
String echo(String input); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>tomcat-communicate</groupId> | |
<artifactId>tomcat-communicate</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
</parent> | |
<artifactId>api.jar</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>tomcat-communicate</groupId> | |
<artifactId>tomcat-communicate</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>pom</packaging> | |
<modules> | |
<module>api.jar</module> | |
<module>service.war</module> | |
<module>ui.war</module> | |
</modules> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.service; | |
import org.springframework.jmx.export.annotation.ManagedOperation; | |
import org.springframework.jmx.export.annotation.ManagedResource; | |
import javax.annotation.PostConstruct; | |
@ManagedResource(objectName = EchoService.MBEAN_NAME) | |
public class EchoServiceImpl implements EchoService { | |
@Override | |
@ManagedOperation | |
public String echo(String input) { | |
return "You said: " + input; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>tomcat-communicate</groupId> | |
<artifactId>tomcat-communicate</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
</parent> | |
<artifactId>service.war</artifactId> | |
<packaging>war</packaging> | |
<dependencies> | |
<dependency> | |
<groupId>tomcat-communicate</groupId> | |
<artifactId>api.jar</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>3.2.8.RELEASE</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> | |
<context:annotation-config/> | |
<context:mbean-export/> | |
<bean id="echoService" class="com.example.service.EchoServiceImpl"/> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | |
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
version="3.0"> | |
<display-name>Servlet 3.0 Web Application</display-name> | |
<context-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value>classpath:/services-context.xml</param-value> | |
</context-param> | |
<listener> | |
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |
</listener> | |
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.web; | |
import com.example.service.EchoService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.context.support.SpringBeanAutowiringSupport; | |
import javax.servlet.ServletConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
@WebServlet(urlPatterns = "/echo/*") | |
public class EchoServlet extends HttpServlet { | |
@Autowired | |
private EchoService echoService; | |
@Override | |
public void init(ServletConfig config) throws ServletException { | |
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, | |
config.getServletContext()); | |
System.out.println("Servlet initialized"); | |
} | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
final String input = req.getPathInfo(); | |
final String output = echoService.echo(input); | |
resp.setBufferSize(1024); | |
resp.setStatus(HttpServletResponse.SC_OK); | |
resp.setContentType("text/plain"); | |
resp.setCharacterEncoding("UTF-8"); | |
resp.getWriter().write(output); | |
resp.getWriter().close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>tomcat-communicate</groupId> | |
<artifactId>tomcat-communicate</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
</parent> | |
<artifactId>ui.war</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>3.2.8.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>tomcat-communicate</groupId> | |
<artifactId>api.jar</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>3.1.0</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:util="http://www.springframework.org/schema/util" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> | |
<bean id="echoService" class="org.springframework.jmx.access.MBeanProxyFactoryBean" lazy-init="true"> | |
<property name="objectName"> | |
<util:constant static-field="com.example.service.EchoService.MBEAN_NAME"/> | |
</property> | |
<property name="proxyInterface" value="com.example.service.EchoService"/> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | |
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
version="3.0"> | |
<display-name>Servlet 3.0 Web Application</display-name> | |
<context-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value>WEB-INF/web-context.xml</param-value> | |
</context-param> | |
<listener> | |
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |
</listener> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://konstantinpavlov.net/post/85202325110/webapp-inter-communication-in-tomcat
When those two webapps are deployed to tomcat, a new JMX bean is created:
and service is accessable from the servlet: