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

  1. 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 the com.camunda.training package and name it TwitterService.

    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);
      }
    }
    
  2. Create a new Java class in your project under src/main/java using the com.camunda.training package and name it CreateTweetDelegate. It must implement the JavaDelegate interface which the sample code in the next step utilizes.

  3. 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);
      }
    }
    
  4. Change the Script Task on the affirmative sequence Publish tweet into a Service Task.

  5. In the Properties Panel of the Publish tweet task select Java Class for the Implementation and then enter the fully qualified name of CreateTweetDelegate as its implementation.

  6. 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.

On this Page: