INTSAMPLES-119 Update Monitor for Twitter OAuth

Twitter now requires authentication.

Disable the real twitter adapter by default; enable it
with spring.profile "twitter" - requires the user to get
real oauth tokens.

Replicate oauth setup instructions from the twitter sample.

Add note to README for enabling the real twitter adapter.

Add .gitignore so committers can keep their real credentials
in, say, 'oauth.properties.foo'.

Add note about `SpringIntegrationTest`.

Update dummy tweets to use new icon.

INTSAMPLES-119 Polishing

- Fix 'twitterTemplate' bean name
- Add content to log and JMX Notification

JIRA: https://jira.springsource.org/browse/INTSAMPLES-119
This commit is contained in:
Gary Russell
2013-11-19 14:09:55 +02:00
committed by Artem Bilan
parent 630d1cf009
commit e190c160f1
6 changed files with 88 additions and 8 deletions

1
intermediate/monitoring/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
src/main/resources/oauth.properties.*

View File

@@ -22,6 +22,8 @@ and then use [VisualVM][]/JConsole to explore the [MBeans][].
The twitter search results can be examined at `http://localhost:8080/monitoring`.
Alternatively, there is a simple `main` class `SpringIntegrationTest`.
## 2.2 Updates:
The application context now includes an example of <int-jmx:notification-publishing-channel-adapter/>, 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.
@@ -39,6 +41,41 @@ You should observe log output of the notifications as well as polling the channe
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.
## Note:
Twitter now requires an authenticated user to perform searches. By default, this project now uses a dummy adapter to avoid having
to configure the credentials. To use a real
adapter, uncomment the __spring.profiles.active__ `context-param` element in the `web.xml`.
To run the `SpringIntegrationTest` command-line application, use `-Dspring.profiles.active=twitter` command line argument in the launch configuration.
However, you will need to configure OAuth and set the values in the OAuth properties.
To use OAuth authentication/authorization with Twitter you must create a new Application on the Twitter Developer's site.
Follow the directions below to create a new application and obtain the consumer keys and the access token:
* Go to [http://dev.twitter.com/](http://dev.twitter.com/)
* Log in to your account
* Go to *My applications*.
* Click on 'Create a new application' link and fill out all required fields on the form provided;
* Submit the form.
* If everything is successful you'll be presented with the 'Consumer Key' and 'Consumer Secret'.
* Copy both values to a safe place.
* On the same page you should see 'My Access Token' button on bottom of the page.
* Click on it and you'll be presented with two more values: 'Access Token' and 'Access Token Secret'.
* Copy these values to a safe place as well.
When done, fill out **oauth.properties** file so it looks similar to this.
twitter.oauth.consumerKey=4XzBPabcJQxyBzzzH3TrRQ
twitter.oauth.consumerSecret=ab2piKdMfPu8bVa3ab6DAIvIWEVZyMDL0RSEN2I8
twitter.oauth.accessToken=21691649-4XYZY5iJEOfz2A9qCFd9SjBRGb3HLmIm4HNE6AMv4
twitter.oauth.accessTokenSecret=AbRxUAvyNCtqQtvxFK8w5ZMtMj20KFhB6oEfTA0
NOTE: the above values are samples only.
--------------------------------------------------------------------------------
For help please see the Spring Integration documentation:

View File

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

View File

@@ -23,12 +23,19 @@
<context:component-scan base-package="org.springframework.integration.service" />
<int-twitter:search-inbound-channel-adapter id="twitter" query="#springintegration"
channel="twitterChannel" auto-startup="true">
<int:poller fixed-rate="30000" max-messages-per-poll="10" />
</int-twitter:search-inbound-channel-adapter>
<!-- As of Twitter API version 1.1, users must be authenticated to perform searches
so, by default, we run with a dummy adapter only see README if you want a real twitter search
where this adapter will be replaced by a real one -->
<int:inbound-channel-adapter id="dummyAdapter" channel="twitterChannel" method="getTweet">
<int:inbound-channel-adapter id="twitter" channel="twitterChannel" method="getTweet">
<bean class="org.springframework.integration.service.impl.DummyTwitter"/>
<int:poller fixed-delay="30000"/>
</int:inbound-channel-adapter>
<!-- This dummy adapter is used to generate traffic for the monitoring demo in addition to the
above adapter, or a real twitter adapter -->
<int:inbound-channel-adapter id="dummyTwitter" channel="twitterChannel" method="getTweet">
<bean class="org.springframework.integration.service.impl.DummyTwitter"/>
<int:poller fixed-delay="5000"/>
</int:inbound-channel-adapter>
@@ -66,7 +73,7 @@
<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" />
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser + '; Content: ' + payload.text" />
<int:bridge id="bridgeToQueueChannel"
input-channel="logger" output-channel="queue"/>
@@ -78,7 +85,7 @@
<int:transformer id="tweetToStringTransformer"
input-channel="twitterChannel"
output-channel="twitterPublishChannel"
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser"/>
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser + '; Content: ' + payload.text"/>
<int:channel id="twitterPublishChannel"/>
@@ -91,4 +98,27 @@
<int:control-bus id="controlBus" input-channel="controlBusChannel"/>
<beans profile="twitter">
<!-- As of Twitter API version 1.1, users must be authenticated to perform searches -->
<context:property-placeholder location="classpath:oauth.properties"/>
<bean id="twitterTemplate"
class="org.springframework.social.twitter.api.impl.TwitterTemplate">
<constructor-arg value="${twitter.oauth.consumerKey}" />
<constructor-arg value="${twitter.oauth.consumerSecret}" />
<constructor-arg value="${twitter.oauth.accessToken}" />
<constructor-arg value="${twitter.oauth.accessTokenSecret}" />
</bean>
<int-twitter:search-inbound-channel-adapter id="twitter" query="#springintegration"
twitter-template="twitterTemplate"
channel="twitterChannel"
auto-startup="true">
<int:poller fixed-rate="30000" max-messages-per-poll="10" />
</int-twitter:search-inbound-channel-adapter>
</beans>
</beans>

View File

@@ -0,0 +1,4 @@
twitter.oauth.consumerKey=
twitter.oauth.consumerSecret=
twitter.oauth.accessToken=
twitter.oauth.accessTokenSecret=

View File

@@ -9,6 +9,14 @@
<param-value>classpath:META-INF/spring/integration/spring-integration-context.xml</param-value>
</context-param>
<!-- uncomment this if you want to use real twitter and have set oauth keys -->
<!--
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>twitter</param-value>
</context-param>
-->
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>