Add a Java Delegate to Publish on Twitter
Goal
The Goal of this lab is to use a JavaDelegate in the service task to publish on twitter.
Short description
- Create a TwitterService class that prints a content to the console.
- Create a Class implementing the JavaDelegate interface.
- Add the code to create a constant content and call your TwitterService.
- Configure this class in the service task.
- Test your implementation with the JUnit test.
Detailed steps
As the Twitter stopped the free access to its API, we have to create our own TwitterService. It should just log our tweet to the console. Create a new Java class in your project under
src/main/java
using thecom.camunda.training
package and name itTwitterService
.import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TwitterService { private final Logger LOGGER = LoggerFactory.getLogger(CreateTweetDelegate.class.getName()); public void updateStatus(String content) { LOGGER.info("Tweet: " + content); } }
Create a new Java class in your project under
src/main/java
using thecom.camunda.training
package and name itCreateTweetDelegate
. It must implement theJavaDelegate
interface which the sample code in the next step utilizes.The example code below shows how to call your TwitterService and use it to tweet.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CreateTweetDelegate implements JavaDelegate { private final Logger LOGGER = LoggerFactory.getLogger(CreateTweetDelegate.class.getName()); TwitterService twitter = new TwitterService(); public void execute(DelegateExecution execution) throws Exception { String content = "I did it! Cheers YOUR NAME HERE"; twitter.updateStatus(content); } }
Change the Script Task on the affirmative sequence
Publish tweet
into a Service Task.In the Properties Panel of the
Publish tweet
task select Java Class for the Implementation and then enter the fully qualified name ofCreateTweetDelegate
as its implementation.Run your testHappyPath JUnit test, you should once again see a green bar in the JUnit window and your tweet should be logged in the console.