Skip to content

Commit e190c16

Browse files
garyrussellArtem Bilan
authored andcommitted
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
1 parent 630d1cf commit e190c16

File tree

6 files changed

+88
-8
lines changed

6 files changed

+88
-8
lines changed

intermediate/monitoring/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/main/resources/oauth.properties.*

intermediate/monitoring/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and then use [VisualVM][]/JConsole to explore the [MBeans][].
2222

2323
The twitter search results can be examined at `http://localhost:8080/monitoring`.
2424

25+
Alternatively, there is a simple `main` class `SpringIntegrationTest`.
26+
2527
## 2.2 Updates:
2628

2729
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
3941

4042
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.
4143

44+
## Note:
45+
46+
Twitter now requires an authenticated user to perform searches. By default, this project now uses a dummy adapter to avoid having
47+
to configure the credentials. To use a real
48+
adapter, uncomment the __spring.profiles.active__ `context-param` element in the `web.xml`.
49+
50+
To run the `SpringIntegrationTest` command-line application, use `-Dspring.profiles.active=twitter` command line argument in the launch configuration.
51+
52+
However, you will need to configure OAuth and set the values in the OAuth properties.
53+
54+
To use OAuth authentication/authorization with Twitter you must create a new Application on the Twitter Developer's site.
55+
Follow the directions below to create a new application and obtain the consumer keys and the access token:
56+
57+
* Go to [http://dev.twitter.com/](http://dev.twitter.com/)
58+
* Log in to your account
59+
* Go to *My applications*.
60+
* Click on 'Create a new application' link and fill out all required fields on the form provided;
61+
* Submit the form.
62+
* If everything is successful you'll be presented with the 'Consumer Key' and 'Consumer Secret'.
63+
* Copy both values to a safe place.
64+
* On the same page you should see 'My Access Token' button on bottom of the page.
65+
* Click on it and you'll be presented with two more values: 'Access Token' and 'Access Token Secret'.
66+
* Copy these values to a safe place as well.
67+
68+
When done, fill out **oauth.properties** file so it looks similar to this.
69+
70+
twitter.oauth.consumerKey=4XzBPabcJQxyBzzzH3TrRQ
71+
twitter.oauth.consumerSecret=ab2piKdMfPu8bVa3ab6DAIvIWEVZyMDL0RSEN2I8
72+
twitter.oauth.accessToken=21691649-4XYZY5iJEOfz2A9qCFd9SjBRGb3HLmIm4HNE6AMv4
73+
twitter.oauth.accessTokenSecret=AbRxUAvyNCtqQtvxFK8w5ZMtMj20KFhB6oEfTA0
74+
75+
NOTE: the above values are samples only.
76+
77+
78+
4279
--------------------------------------------------------------------------------
4380

4481
For help please see the Spring Integration documentation:

intermediate/monitoring/src/main/java/org/springframework/integration/service/impl/DummyTwitter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Tweet getTweet() {
3333
"Spring Integration is the coolest Enterprise Integration project",
3434
new Date(),
3535
"SomeUser",
36-
"http://a0.twimg.com/profile_images/1598911687/ICO_S2_Bug_normal.png",
36+
"https://pbs.twimg.com/profile_images/378800000502646541/992d3596458fca87741b8e93e7df0860_normal.png",
3737
0L,
3838
0L,
3939
null,

intermediate/monitoring/src/main/resources/META-INF/spring/integration/spring-integration-context.xml

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@
2323

2424
<context:component-scan base-package="org.springframework.integration.service" />
2525

26-
<int-twitter:search-inbound-channel-adapter id="twitter" query="#springintegration"
27-
channel="twitterChannel" auto-startup="true">
28-
<int:poller fixed-rate="30000" max-messages-per-poll="10" />
29-
</int-twitter:search-inbound-channel-adapter>
26+
<!-- As of Twitter API version 1.1, users must be authenticated to perform searches
27+
so, by default, we run with a dummy adapter only see README if you want a real twitter search
28+
where this adapter will be replaced by a real one -->
3029

31-
<int:inbound-channel-adapter id="dummyAdapter" channel="twitterChannel" method="getTweet">
30+
<int:inbound-channel-adapter id="twitter" channel="twitterChannel" method="getTweet">
31+
<bean class="org.springframework.integration.service.impl.DummyTwitter"/>
32+
<int:poller fixed-delay="30000"/>
33+
</int:inbound-channel-adapter>
34+
35+
<!-- This dummy adapter is used to generate traffic for the monitoring demo in addition to the
36+
above adapter, or a real twitter adapter -->
37+
38+
<int:inbound-channel-adapter id="dummyTwitter" channel="twitterChannel" method="getTweet">
3239
<bean class="org.springframework.integration.service.impl.DummyTwitter"/>
3340
<int:poller fixed-delay="5000"/>
3441
</int:inbound-channel-adapter>
@@ -66,7 +73,7 @@
6673
<int:publish-subscribe-channel id="logger" />
6774

6875
<int:logging-channel-adapter id="loggingAdapter" channel="logger" level="INFO"
69-
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser" />
76+
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser + '; Content: ' + payload.text" />
7077

7178
<int:bridge id="bridgeToQueueChannel"
7279
input-channel="logger" output-channel="queue"/>
@@ -78,7 +85,7 @@
7885
<int:transformer id="tweetToStringTransformer"
7986
input-channel="twitterChannel"
8087
output-channel="twitterPublishChannel"
81-
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser"/>
88+
expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser + '; Content: ' + payload.text"/>
8289

8390
<int:channel id="twitterPublishChannel"/>
8491

@@ -91,4 +98,27 @@
9198

9299
<int:control-bus id="controlBus" input-channel="controlBusChannel"/>
93100

101+
<beans profile="twitter">
102+
103+
<!-- As of Twitter API version 1.1, users must be authenticated to perform searches -->
104+
105+
<context:property-placeholder location="classpath:oauth.properties"/>
106+
107+
<bean id="twitterTemplate"
108+
class="org.springframework.social.twitter.api.impl.TwitterTemplate">
109+
<constructor-arg value="${twitter.oauth.consumerKey}" />
110+
<constructor-arg value="${twitter.oauth.consumerSecret}" />
111+
<constructor-arg value="${twitter.oauth.accessToken}" />
112+
<constructor-arg value="${twitter.oauth.accessTokenSecret}" />
113+
</bean>
114+
115+
<int-twitter:search-inbound-channel-adapter id="twitter" query="#springintegration"
116+
twitter-template="twitterTemplate"
117+
channel="twitterChannel"
118+
auto-startup="true">
119+
<int:poller fixed-rate="30000" max-messages-per-poll="10" />
120+
</int-twitter:search-inbound-channel-adapter>
121+
122+
</beans>
123+
94124
</beans>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
twitter.oauth.consumerKey=
2+
twitter.oauth.consumerSecret=
3+
twitter.oauth.accessToken=
4+
twitter.oauth.accessTokenSecret=

intermediate/monitoring/src/main/webapp/WEB-INF/web.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
<param-value>classpath:META-INF/spring/integration/spring-integration-context.xml</param-value>
1010
</context-param>
1111

12+
<!-- uncomment this if you want to use real twitter and have set oauth keys -->
13+
<!--
14+
<context-param>
15+
<param-name>spring.profiles.active</param-name>
16+
<param-value>twitter</param-value>
17+
</context-param>
18+
-->
19+
1220
<!-- Creates the Spring Container shared by all Servlets and Filters -->
1321
<listener>
1422
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

0 commit comments

Comments
 (0)