- 浏览: 2539914 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
MINA(4)JMX with Spring
Using JMX with Spring Bean, here is my spring bean CaculateImpl, this may be my business logic bean.
package com.sillycat.easynio.plugins.jmx;
public class CaculateImpl implements CaculateInterface{
public int add(int x, int y) {
return x + y;
}
public String echo(String message) {
return "Hello, it is message from JMX = " + message;
}
public String securit() {
return "forbidden!";
}
}
There are 3 method in this bean, I only want to expose 2 of them.
The orignial interface for this business bean is
package com.sillycat.easynio.plugins.jmx;
public interface CaculateInterface {
public int add(int x, int y);
public String echo(String message);
public String securit();
}
The only interface I want to open to JMX.
package com.sillycat.easynio.plugins.jmx;
public interface CaculateJMXInterface {
public int add(int x, int y);
public String echo(String message);
}
The dependecies in pom.xml are as follow:
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-tools</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
The JMX Spring Configuration file jmx-context.xml:
<bean id="caculate" class="com.sillycat.easynio.plugins.jmx.CaculateImpl">
</bean>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="com.sillycat:caculate=CaculateJMX" value-ref="caculate" />
<entry key="mx4j:name=HttpAdaptor" value-ref="httpAdaptor" />
</map>
</property>
<property name="assembler" ref="assembler" />
</bean>
<bean id="assembler"
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<list>
<value>com.sillycat.easynio.plugins.jmx.CaculateJMXInterface</value>
</list>
</property>
</bean>
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port">
<value>9875</value>
</property>
</bean>
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName">
<value>connector:name=rmi</value>
</property>
<property name="serviceUrl">
<value>service:jmx:rmi://localhost/jndi/rmi://localhost:9875/jmxrmi</value>
</property>
</bean>
We can use the proxy client to manage the JMX bean, jmxclient-context.xml:
<bean id="caculateJMXClient" class="org.springframework.jmx.access.MBeanProxyFactoryBean">
<property name="connectOnStartup" value="true" />
<property name="objectName" value="com.sillycat:caculate=CaculateJMX" />
<property name="proxyInterface">
<value>com.sillycat.easynio.plugins.jmx.CaculateJMXInterface</value>
</property>
<property name="serviceUrl">
<value>service:jmx:rmi://localhost/jndi/rmi://localhost:9875/jmxrmi</value>
</property>
</bean>
We can call the JMX bean using this client class:
package com.sillycat.easynio.plugins.jmx;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/test-context.xml" })
public class CaculateJMXClientTest {
@Autowired
@Qualifier("caculateJMXClient")
private CaculateJMXInterface caculateJMXClient;
@Test
public void dumy() {
Assert.assertTrue(true);
}
@Test
public void add() {
int sum = caculateJMXClient.add(100, 200);
Assert.assertEquals(sum, 300);
}
@Test
public void echo(){
System.out.println(caculateJMXClient.echo("sillycat"));
}
}
Easily, we can use a mx4j adaptor to make the end user to use browsers.
mx4j-context.xml:
<bean id="httpAdaptor" class="mx4j.tools.adaptor.http.HttpAdaptor">
<property name="processor">
<bean id="xsltProcessor" class="mx4j.tools.adaptor.http.XSLTProcessor"/>
</property>
<property name="host">
<value>localhost</value>
</property>
<property name="port">
<value>8000</value>
</property>
</bean>
We need using this kind of application to star the adaptor server, that is weird.
public class MX4JClientMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"main-context.xml");
HttpAdaptor httpAdaptor = (HttpAdaptor) ctx.getBean("httpAdaptor");
try {
httpAdaptor.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Then we can visit the URL as follow:
http://localhost:8000/serverbydomain
Filter: *:*
Filter: com.sillycat:*, click the Query button.
We can using the JMX bean now.
I change the spring startup listener to start the adaptor server instead of application:
package com.sillycat.easynio.plugins.jmx;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import mx4j.tools.adaptor.http.HttpAdaptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class JMXContextLoadListener extends ContextLoaderListener implements
ServletContextListener {
private final Log log = LogFactory.getLog(getClass());
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ServletContext context = event.getServletContext();
setupContext(context);
}
protected void setupContext(final ServletContext context) {
ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
HttpAdaptor httpAdaptor = (HttpAdaptor) ctx.getBean("httpAdaptor");
try {
httpAdaptor.start();
} catch (IOException e) {
log.error(e);
}
}
}
<listener>
<listener-class>com.sillycat.easynio.plugins.jmx.JMXContextLoadListener</listener-class>
</listener>
references:
http://blog.csdn.net/shirdrn/article/details/6358688
http://topmanopensource.iteye.com/blog/832848
http://topmanopensource.iteye.com/blog/832851
http://topmanopensource.iteye.com/blog/832855
Using JMX with Spring Bean, here is my spring bean CaculateImpl, this may be my business logic bean.
package com.sillycat.easynio.plugins.jmx;
public class CaculateImpl implements CaculateInterface{
public int add(int x, int y) {
return x + y;
}
public String echo(String message) {
return "Hello, it is message from JMX = " + message;
}
public String securit() {
return "forbidden!";
}
}
There are 3 method in this bean, I only want to expose 2 of them.
The orignial interface for this business bean is
package com.sillycat.easynio.plugins.jmx;
public interface CaculateInterface {
public int add(int x, int y);
public String echo(String message);
public String securit();
}
The only interface I want to open to JMX.
package com.sillycat.easynio.plugins.jmx;
public interface CaculateJMXInterface {
public int add(int x, int y);
public String echo(String message);
}
The dependecies in pom.xml are as follow:
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-tools</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
The JMX Spring Configuration file jmx-context.xml:
<bean id="caculate" class="com.sillycat.easynio.plugins.jmx.CaculateImpl">
</bean>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="com.sillycat:caculate=CaculateJMX" value-ref="caculate" />
<entry key="mx4j:name=HttpAdaptor" value-ref="httpAdaptor" />
</map>
</property>
<property name="assembler" ref="assembler" />
</bean>
<bean id="assembler"
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<list>
<value>com.sillycat.easynio.plugins.jmx.CaculateJMXInterface</value>
</list>
</property>
</bean>
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port">
<value>9875</value>
</property>
</bean>
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName">
<value>connector:name=rmi</value>
</property>
<property name="serviceUrl">
<value>service:jmx:rmi://localhost/jndi/rmi://localhost:9875/jmxrmi</value>
</property>
</bean>
We can use the proxy client to manage the JMX bean, jmxclient-context.xml:
<bean id="caculateJMXClient" class="org.springframework.jmx.access.MBeanProxyFactoryBean">
<property name="connectOnStartup" value="true" />
<property name="objectName" value="com.sillycat:caculate=CaculateJMX" />
<property name="proxyInterface">
<value>com.sillycat.easynio.plugins.jmx.CaculateJMXInterface</value>
</property>
<property name="serviceUrl">
<value>service:jmx:rmi://localhost/jndi/rmi://localhost:9875/jmxrmi</value>
</property>
</bean>
We can call the JMX bean using this client class:
package com.sillycat.easynio.plugins.jmx;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/test-context.xml" })
public class CaculateJMXClientTest {
@Autowired
@Qualifier("caculateJMXClient")
private CaculateJMXInterface caculateJMXClient;
@Test
public void dumy() {
Assert.assertTrue(true);
}
@Test
public void add() {
int sum = caculateJMXClient.add(100, 200);
Assert.assertEquals(sum, 300);
}
@Test
public void echo(){
System.out.println(caculateJMXClient.echo("sillycat"));
}
}
Easily, we can use a mx4j adaptor to make the end user to use browsers.
mx4j-context.xml:
<bean id="httpAdaptor" class="mx4j.tools.adaptor.http.HttpAdaptor">
<property name="processor">
<bean id="xsltProcessor" class="mx4j.tools.adaptor.http.XSLTProcessor"/>
</property>
<property name="host">
<value>localhost</value>
</property>
<property name="port">
<value>8000</value>
</property>
</bean>
We need using this kind of application to star the adaptor server, that is weird.
public class MX4JClientMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"main-context.xml");
HttpAdaptor httpAdaptor = (HttpAdaptor) ctx.getBean("httpAdaptor");
try {
httpAdaptor.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Then we can visit the URL as follow:
http://localhost:8000/serverbydomain
Filter: *:*
Filter: com.sillycat:*, click the Query button.
We can using the JMX bean now.
I change the spring startup listener to start the adaptor server instead of application:
package com.sillycat.easynio.plugins.jmx;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import mx4j.tools.adaptor.http.HttpAdaptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class JMXContextLoadListener extends ContextLoaderListener implements
ServletContextListener {
private final Log log = LogFactory.getLog(getClass());
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ServletContext context = event.getServletContext();
setupContext(context);
}
protected void setupContext(final ServletContext context) {
ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
HttpAdaptor httpAdaptor = (HttpAdaptor) ctx.getBean("httpAdaptor");
try {
httpAdaptor.start();
} catch (IOException e) {
log.error(e);
}
}
}
<listener>
<listener-class>com.sillycat.easynio.plugins.jmx.JMXContextLoadListener</listener-class>
</listener>
references:
http://blog.csdn.net/shirdrn/article/details/6358688
http://topmanopensource.iteye.com/blog/832848
http://topmanopensource.iteye.com/blog/832851
http://topmanopensource.iteye.com/blog/832855
发表评论
-
Update Site will come soon
2021-06-02 04:10 1672I am still keep notes my tech n ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 288Hadoop Docker 2019 Version 3.2. ... -
Nginx and Proxy 2019(1)Nginx Enable Lua and Parse JSON
2019-12-03 04:17 437Nginx and Proxy 2019(1)Nginx En ... -
Data Solution 2019(13)Docker Zeppelin Notebook and Memory Configuration
2019-11-09 07:15 279Data Solution 2019(13)Docker Ze ... -
Data Solution 2019(10)Spark Cluster Solution with Zeppelin
2019-10-29 08:37 243Data Solution 2019(10)Spark Clu ... -
AMAZON Kinesis Firehose 2019(1)Firehose Buffer to S3
2019-10-01 10:15 313AMAZON Kinesis Firehose 2019(1) ... -
Rancher and k8s 2019(3)Clean Installation on CentOS7
2019-09-19 23:25 301Rancher and k8s 2019(3)Clean In ... -
Pacemaker 2019(1)Introduction and Installation on CentOS7
2019-09-11 05:48 333Pacemaker 2019(1)Introduction a ... -
Crontab-UI installation and Introduction
2019-08-30 05:54 442Crontab-UI installation and Int ... -
Spiderkeeper 2019(1)Installation and Introduction
2019-08-29 06:49 493Spiderkeeper 2019(1)Installatio ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 2019(2)Ubuntu and Mu ... -
Supervisor 2019(1)CentOS 7
2019-08-19 09:33 321Supervisor 2019(1)CentOS 7 Ins ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 365Redis Cluster 2019(3)Redis Clus ... -
Amazon Lambda and Version Limit
2019-08-02 01:42 432Amazon Lambda and Version Limit ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 507MySQL HA Solution 2019(1)Master ... -
RabbitMQ Cluster 2019(2)Cluster HA and Proxy
2019-07-11 12:41 456RabbitMQ Cluster 2019(2)Cluster ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:35 315Running Zeppelin with Nginx Aut ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:34 316Running Zeppelin with Nginx Aut ... -
ElasticSearch(3)Version Upgrade and Cluster
2019-05-20 05:00 320ElasticSearch(3)Version Upgrade ... -
Jetty Server and Cookie Domain Name
2019-04-28 23:59 389Jetty Server and Cookie Domain ...
相关推荐
《Mina2与Spring整合应用实战》 在Java开发领域,Apache Mina和Spring框架的结合使用能够构建高效、可扩展的网络应用。Mina2作为一个轻量级的网络通信框架,提供了高度抽象的API,使得开发者可以方便地处理网络I/O...
4. **协议编码解码器**:用于将网络传输的数据转换为业务对象,反之亦然,可能实现了Mina的ProtocolCodec接口。 5. **测试用例**:验证服务端功能的JUnit或其他测试框架的测试类。 通过这个项目,开发者可以学习到...
4. **MINA与Spring的整合**:将MINA与Spring结合,可以利用Spring的管理功能来配置MINA的服务器和客户端,如Bean的生命周期管理、AOP切面等。这使得MINA应用更易于配置、测试和维护。整合过程通常包括在Spring配置...
4. Mina-Spring整合 Spring的IoC容器可以管理Mina服务端或客户端的生命周期,使得Mina的配置变得更加简单和灵活。通过Spring的Bean定义,我们可以配置Mina的Acceptor、ProtocolDecoder、ProtocolEncoder等组件,实现...
**基于Spring的Mina框架详解** Mina框架是一款高性能、轻量级的网络通信框架,主要应用于开发基于TCP和UDP的网络应用。它提供了一种简单而强大的编程模型,使得开发者能够快速构建出稳定且高效的网络服务。在Spring...
该项目是本人真实项目中使用到的mina + spring+struts2 + mybatis框架,里面有详细的注释说明,以及一个完整的接收消息、入库、返回例子。对需要真实项目开发的人员来说,可以直接使用。 另外,如果需要更加详细的...
4. **在Spring Boot应用中使用** 在Spring Boot的主类或者服务类中,我们可以注入`MinaSerialHandler`实例,然后调用`sendData()`方法来发送数据,或者在其他地方监听`messageReceived()`方法来处理接收到的数据。 ...
Spring 和 Mina 结合使用可以构建高性能的网络通信应用,而Spring MVC 是Spring 框架的一个模块,专门用于处理Web请求。在这个"spring+mina实现http接口服务端通信客户端"的项目中,我们将深入探讨如何整合这两个...
mina-integration-jmx-2.0.2.jar
在本文中,我们将深入探讨如何将Spring Boot与Mina进行深度整合,以便为新手开发者提供一个开箱即用的解决方案。Spring Boot以其简洁的配置和快速的开发体验,已经成为Java领域中的主流微服务框架,而Mina则是一个...
标题中的"SSI+Mina2(Struts2+Spring4+Mybatis3+Mina2)集成发布就可运行"指的是一个基于Java的Web应用程序开发框架的整合,它结合了多种技术来构建高效、灵活和可扩展的网络应用。这个集成方案主要包括以下组件: 1....
之前的项目需要用到mina,实现的功能主要是:服务端主动发送消息到客户端,这个的服务端为外网的tomcat,客户端为内网的tomcat,由于无法知道内网tomcat 的地址,也就不能直接通过http的方式发送信息回来,最后想来...
而SpringBoot是基于Spring框架的微服务开发工具,简化了Spring应用的初始搭建以及开发过程。下面我们将详细讨论如何在SpringBoot项目中整合Mina,并自定义解码器。 首先,我们需要在SpringBoot项目中添加Mina的相关...
标题中的"mina 与spring的结合开发,包头指令"可能是指在使用MINA构建网络服务时,如何利用Spring框架来管理和配置MINA的应用上下文,以及如何通过Spring的配置来定义和处理网络通信中的包头信息。 在MINA中,数据...
在构建Java企业级应用时,整合Apache MINA、Spring和Hibernate是常见的技术栈选择。这三个框架分别在不同的层面上提供服务:MINA为网络通信,Spring作为应用框架,而Hibernate则是对象关系映射(ORM)工具。以下是...
spring结合Mina的配置文件,设计到spring构造 属性编辑器
mina新手案例,mina新手教程源码 mina+springboot最简单的案例。用的IDEA * mina服务端 * 1、添加@Controller注解和 @PostConstruct注解,代表启动springboot项目时也调用该类下的该方法, * 启动springboot项目...
在Java开发领域,Spring、MyBatis和Mina都是非常重要的框架。Spring作为一个全面的轻量级应用框架,提供依赖注入、AOP(面向切面编程)、MVC等核心功能,广泛应用于服务端应用程序的开发。MyBatis则是一个优秀的持久...
2.系统读取mina-spring.xml。如果想把该工程导成jar则需要处理下配置文件的路径 3.结合自己系统时(导成jar集成进自己系统)需要继承与serverHandler,实现里面doService方法 4.本系统支持HTTP以及TCP链接,这个是...
**标题解析:**"Mina 集成Spring 传输文件和文本" Mina(Java Foundation for Network Applications)是一个开源的网络通信框架,它提供了一种高性能、高灵活性的网络应用开发方式。集成Spring框架后,可以利用...