INTSAMPLES-95 Add Remote Monitor

INTSAMPLES-95 Add OperationInvokingAdapter

For reference see: https://jira.springsource.org/browse/INTSAMPLES-95
This commit is contained in:
Gary Russell
2012-10-11 12:33:38 -04:00
committed by Gunnar Hillert
parent 1a2ecaf254
commit ee8de8c6b0
11 changed files with 346 additions and 199 deletions

View File

@@ -5,21 +5,39 @@ This application demonstrates managing and monitoring Spring Integration Applica
It is based on the STS 'Spring Integration Project (war)' template project, available using
New... | Spring Template Project
New... | Spring Template Project
It was used in the "Managing and Monitoring Spring Integration" webinar available
on the SpringSource Developer YouTube Channel http://www.youtube.com/SpringSourceDev
It was used in the **Managing and Monitoring Spring Integration** webinar available on the [SpringSource Developer YouTube Channel][].
If you wish to see the changes made during the webinar, please use the following git command:
git log -p <The commit titled 'Webinar End State'>
git log -p <The commit titled 'Webinar End State'>
To run the sample, in STS, use Run As... | Run on Server
To run the sample, in STS, use:
and then use VisualVM/JConsole to explore the MBeans.
Run As... | Run on Server
The twitter search results can be examined at http://localhost:8080/monitoring
and then use [VisualVM][]/JConsole to explore the [MBeans][].
The twitter search results can be examined at `http://localhost:8080/monitoring`.
## 2.2 Updates:
The application context now includes an example of &lt;int-jmx:notification-publishing-channel-adapter/&gt;, to which the tweets are published. You can navigate to the MBean - **spring.application:type=TweetPublisher,name=tweeter**, click the _Notifications_ tab and then subscribe. You will then see the notifications.
An additional class _NotificationListener_ is now included. It demonstrates the use of &lt;int-jmx:notification-listening-channel-adapter&gt; and &lt;int-jmx:attribute-polling-channel-adapter&gt;.
Also, it shows how to use an &lt;int-jmx:operation-invoking-channel-adapter&gt; to stop/start the
_dummyAdapter_ in the web application.
These adapters use a client connector to connect to the Twitter search web application to catch notifications published by it, polls the _sendCount_ attribute of the _twitterChannel_. and invokes operations on the _dummyAdapter_.
You will need to update the credentials on the _clientConnector_ in _remote-monitor-context.xml_ to match your environment. If you are using STS, you can find the credentials in _Servers | [server] | jmxremote.password_. You can then run the _NotificationListener_'s main method.
You should observe log output of the notifications as well as polling the channel's _sendCount_. Simply use the console to enter 'n' to stop and 'y' to start the adapter.
These changes show how you can create a sophisticated monitoring application using Spring Integration - it is important to understand that the application being monitored doesn't have to be a Spring or Spring Integration application - any application that exports MBeans can be monitored in this way.
--------------------------------------------------------------------------------
@@ -27,3 +45,6 @@ For help please see the Spring Integration documentation:
http://www.springsource.org/spring-integration
[MBeans]: http://docs.oracle.com/javase/tutorial/jmx/mbeans/index.html
[SpringSource Developer YouTube Channel]: http://www.youtube.com/SpringSourceDev
[VisualVM]: http://visualvm.java.net/

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2002-2010 the original author or authors
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.model;
@@ -22,109 +22,109 @@ import java.util.Date;
*/
public class TwitterMessage {
private Date createdAt;
private String text;
private String fromUser;
private String profileImageUrl;
private Date createdAt;
private String text;
private String fromUser;
private String profileImageUrl;
/** Default constructor. */
public TwitterMessage() {
super();
}
/** Default constructor. */
public TwitterMessage() {
super();
}
/** Constructor to initialize all fields available. */
public TwitterMessage(Date createdAt, String text, String fromUser,
String profileImageUrl) {
super();
this.createdAt = createdAt;
this.text = text;
this.fromUser = fromUser;
this.profileImageUrl = profileImageUrl;
}
/** Constructor to initialize all fields available. */
public TwitterMessage(Date createdAt, String text, String fromUser,
String profileImageUrl) {
super();
this.createdAt = createdAt;
this.text = text;
this.fromUser = fromUser;
this.profileImageUrl = profileImageUrl;
}
public Date getCreatedAt() {
return createdAt;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public String getText() {
return text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void setText(String text) {
this.text = text;
}
public String getFromUser() {
return fromUser;
}
public String getFromUser() {
return fromUser;
}
public void setFromUser(String fromUser) {
this.fromUser = fromUser;
}
public void setFromUser(String fromUser) {
this.fromUser = fromUser;
}
public String getProfileImageUrl() {
return profileImageUrl;
}
public String getProfileImageUrl() {
return profileImageUrl;
}
public void setProfileImageUrl(String profileImageUrl) {
this.profileImageUrl = profileImageUrl;
}
public void setProfileImageUrl(String profileImageUrl) {
this.profileImageUrl = profileImageUrl;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((createdAt == null) ? 0 : createdAt.hashCode());
result = prime * result
+ ((fromUser == null) ? 0 : fromUser.hashCode());
result = prime * result
+ ((profileImageUrl == null) ? 0 : profileImageUrl.hashCode());
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((createdAt == null) ? 0 : createdAt.hashCode());
result = prime * result
+ ((fromUser == null) ? 0 : fromUser.hashCode());
result = prime * result
+ ((profileImageUrl == null) ? 0 : profileImageUrl.hashCode());
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TwitterMessage other = (TwitterMessage) obj;
if (createdAt == null) {
if (other.createdAt != null)
return false;
} else if (!createdAt.equals(other.createdAt))
return false;
if (fromUser == null) {
if (other.fromUser != null)
return false;
} else if (!fromUser.equals(other.fromUser))
return false;
if (profileImageUrl == null) {
if (other.profileImageUrl != null)
return false;
} else if (!profileImageUrl.equals(other.profileImageUrl))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
return true;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TwitterMessage other = (TwitterMessage) obj;
if (createdAt == null) {
if (other.createdAt != null)
return false;
} else if (!createdAt.equals(other.createdAt))
return false;
if (fromUser == null) {
if (other.fromUser != null)
return false;
} else if (!fromUser.equals(other.fromUser))
return false;
if (profileImageUrl == null) {
if (other.profileImageUrl != null)
return false;
} else if (!profileImageUrl.equals(other.profileImageUrl))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
return true;
}
@Override
public String toString() {
return "Tweet [createdAt=" + createdAt + ", text=" + text
+ ", fromUser=" + fromUser + ", profileImageUrl="
+ profileImageUrl + "]";
}
@Override
public String toString() {
return "Tweet [createdAt=" + createdAt + ", text=" + text
+ ", fromUser=" + fromUser + ", profileImageUrl="
+ profileImageUrl + "]";
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2002-2010 the original author or authors
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.mvc.controller;
@@ -33,56 +33,56 @@ import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HomeController {
private static final Log logger = LogFactory.getLog(HomeController.class);
private static final Log logger = LogFactory.getLog(HomeController.class);
@Autowired
private TwitterService twitterService;
@Autowired
private TwitterService twitterService;
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value="/")
public String home(Model model, @RequestParam(required=false) String startTwitter,
@RequestParam(required=false) String stopTwitter,
@RequestParam(required=false) String shutdown) {
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value="/")
public String home(Model model, @RequestParam(required=false) String startTwitter,
@RequestParam(required=false) String stopTwitter,
@RequestParam(required=false) String shutdown) {
if (startTwitter != null) {
twitterService.startTwitterAdapter();
return "redirect:/";
}
if (startTwitter != null) {
twitterService.startTwitterAdapter();
return "redirect:/";
}
if (stopTwitter != null) {
twitterService.stopTwitterAdapter();
return "redirect:/";
}
if (stopTwitter != null) {
twitterService.stopTwitterAdapter();
return "redirect:/";
}
if (shutdown != null) {
twitterService.shutdown();
return "redirect:/";
}
if (shutdown != null) {
twitterService.shutdown();
return "redirect:/";
}
final Collection<TwitterMessage> twitterMessages = twitterService.getTwitterMessages();
final Collection<TwitterMessage> twitterMessages = twitterService.getTwitterMessages();
logger.info("Retrieved " + twitterMessages.size() + " Twitter messages.");
logger.info("Retrieved " + twitterMessages.size() + " Twitter messages.");
model.addAttribute("twitterMessages", twitterMessages);
model.addAttribute("twitterMessages", twitterMessages);
return "home";
}
return "home";
}
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value="/ajax")
public String ajaxCall(Model model) {
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value="/ajax")
public String ajaxCall(Model model) {
final Collection<TwitterMessage> twitterMessages = twitterService.getTwitterMessages();
final Collection<TwitterMessage> twitterMessages = twitterService.getTwitterMessages();
logger.info("Retrieved " + twitterMessages.size() + " Twitter messages.");
model.addAttribute("twitterMessages", twitterMessages);
logger.info("Retrieved " + twitterMessages.size() + " Twitter messages.");
model.addAttribute("twitterMessages", twitterMessages);
return "twitterMessages";
return "twitterMessages";
}
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2002-2010 the original author or authors
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.service;

View File

@@ -33,7 +33,7 @@ public class DummyTwitter {
"Spring Integration is the coolest Enterprise Integration project",
new Date(),
"SomeUser",
null,
"http://a0.twimg.com/profile_images/1598911687/ICO_S2_Bug_normal.png",
0L,
0L,
null,

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springintegration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* This class demonstrates the use a Spring Integration jmx adadpers to receive
* notifications, poll attributes, and invoke operations on a Remote JMX MBeanServer.
*
* @author Gary Russell
* @since 2.2
*
*/
public class NotificationListener {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/integration/remote-monitor-context.xml");
Gateway gw = ctx.getBean(Gateway.class);
int cmd = 0;
while (cmd != 'q') {
cmd = System.in.read();
gw.send((char) cmd);
}
}
public static interface Gateway {
void send(char command);
}
}

View File

@@ -0,0 +1,63 @@
<?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:int-jmx="http://www.springframework.org/schema/integration/jmx"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd
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="clientConnector" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean">
<property name="serviceUrl" value="service:jmx:rmi://localhost/jndi/rmi://localhost:6969/jmxrmi"/>
<property name="environment">
<map>
<entry key="jmx.remote.credentials" value="#{new String[] {'admin', 'cbycfvjkjjudosrxchsgijfnbsrpnqctnujeirubbacljkhh'}}" />
</map>
</property>
</bean>
<int-jmx:notification-listening-channel-adapter
server="clientConnector"
channel="publishInChannel"
object-name="spring.application:type=TweetPublisher,name=*"/>
<int:channel id="publishInChannel"/>
<int:logging-channel-adapter channel="publishInChannel"/>
<int-jmx:attribute-polling-channel-adapter channel="publishInChannel"
server="clientConnector"
attribute-name="SendCount"
object-name="spring.application:type=MessageChannel,name=twitterChannel">
<int:poller fixed-delay="12000"/>
</int-jmx:attribute-polling-channel-adapter>
<int:gateway service-interface="org.springintegration.NotificationListener$Gateway"
default-request-channel="commandRouter"/>
<int:channel id="commandRouter"/>
<int:router input-channel="commandRouter"
expression="payload"
default-output-channel="nullChannel"
resolution-required="false">
<int:mapping value="y" channel="dummyStarter"/>
<int:mapping value="n" channel="dummyStopper"/>
</int:router>
<int:channel id="dummyStarter"/>
<int:channel id="dummyStopper"/>
<int-jmx:operation-invoking-channel-adapter channel="dummyStopper"
server="clientConnector"
operation-name="stop"
object-name="spring.application:type=MessageSource,name=dummyAdapter,bean=endpoint" />
<int-jmx:operation-invoking-channel-adapter channel="dummyStarter"
server="clientConnector"
operation-name="start"
object-name="spring.application:type=MessageSource,name=dummyAdapter,bean=endpoint" />
</beans>

View File

@@ -49,7 +49,7 @@
<bean id="twitterService" class="org.springintegration.service.impl.DefaultTwitterService"/>
<int:channel id="twitterChannel">
<int:publish-subscribe-channel id="twitterChannel">
<int:interceptors>
<int:wire-tap channel="logger" />
<bean class="org.springintegration.PayloadAwareTimingInterceptor">
@@ -61,19 +61,32 @@
</constructor-arg>
</bean>
</int:interceptors>
</int:channel>
</int:publish-subscribe-channel>
<int:publish-subscribe-channel id="logger" />
<int:logging-channel-adapter id="loggingAdapter" channel="logger" level="INFO"
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser" />
<int:bridge input-channel="logger" output-channel="queue"/>
<int:bridge id="bridgeToQueueChannel"
input-channel="logger" output-channel="queue"/>
<int:channel id="queue">
<int:queue/>
</int:channel>
<int:transformer id="tweetToStringTransformer"
input-channel="twitterChannel"
output-channel="twitterPublishChannel"
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser"/>
<int:channel id="twitterPublishChannel"/>
<int-jmx:notification-publishing-channel-adapter
channel="twitterPublishChannel"
default-notification-type="TWEET"
object-name="spring.application:type=TweetPublisher,name=tweeter"/>
<int:channel id="controlBusChannel"/>
<int:control-bus id="controlBus" input-channel="controlBusChannel"/>

View File

@@ -1,32 +1,37 @@
<?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:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="org.springframework.integration.mvc.controller" />
<!-- Scans within the base package of the application for @Components to
configure as beans -->
<context:component-scan
base-package="org.springframework.integration.mvc.controller" />
<!-- <mvc:default-servlet-handler /> -->
<!-- <mvc:default-servlet-handler /> -->
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/css directory -->
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/css directory -->
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans>

View File

@@ -6,7 +6,7 @@
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p [%.10t][%.10c] %m%n" />
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
</layout>
</appender>

View File

@@ -20,15 +20,16 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Run this class to run the sample from the command line.
*
* @author Gary Russell
*/
public class SpringIntegrationTest {
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", SpringIntegrationTest.class);
System.out.println("Hit Enter to terminate");
System.in.read();
}
new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", SpringIntegrationTest.class);
System.out.println("Hit Enter to terminate");
System.in.read();
}
}