Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Getting Started with Global Beans in Fuse Tooling 10.0.0

August 23, 2017
Brian Fitzpatrick
Related topics:
Developer Tools
Related products:
Developer ToolsRed Hat Fuse

Share:

    Red Hat JBoss Fuse provides an open source, lightweight, modular platform that enables you to connect a variety of services and systems across your application environment. And, included with Red Hat JBoss Developer Studio, is the Fuse Tooling that helps you take advantage of that platform.

    The route editor initially focused on the parts of the Camel configuration inside the route or Camel context element, but in version 8.0.0, we began adding support for global elements such as data formats and endpoints on the Configurations tab. With the 10.0 release, we add support for beans that are outside the route.

    This tutorial shows how to create a customizable service that uses a global Java bean and Camel XML. By the end, you will have a simple Fuse integration project, which includes a Camel test that verifies the bean logic.

    The main steps in this tutorial are:

    1. Create a Fuse integration project.
    2. Create a bean in the project.
    3. Finish the bean class.
    4. Create a route.
    5. Create a test.
    6. Finish the generated test.

    Prerequisites: Eclipse Oxygen is required. During installation of Red Hat Developer Studio 11.0, you should have selected installation of JBoss Fuse Tooling.

    Step 1: Create a Project

    First, we have to create a project for where we can put our code.

    1. In the Developer Studio menu bar, click File->New->Fuse Integration Project.
    2. In the new project wizard, in the Project Name field, enter global-bean-tutorial.
    3. Accept all defaults to create a new, empty Fuse project.
    4. Click Finish.

    Fuse tooling will then build a new Blueprint Camel project, which can take a few moments.

    Step 2: Create our Global Bean

    Create a simple Java bean to use in the Camel route:

    1. In the route editor, click the Configurations tab and then click Add.
    2. In the “Create new global element...” dialog, click Bean and then click OK.
    3. In the “Add Bean” dialog, in the Id field, enter GreetingBean.
    4. To the right of the Class field, click the + button to create a new class.
    5. In the “New Java Class” wizard:
      1. In the Package field, enter org.example.
      2. In the Name field, enter Greeting.
      3. Click Finish.
    6. In the “Add Bean” dialog, click Finish.

    Step 3: Finish the Bean Class

    The previous step created a rough outline for the Java bean. To finish the bean class and make it do something, change the code to something like this:

    package org.example;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Greeting {
    
       private List messageCache = new ArrayList<>();
       private String greetingText = "Hello";
    
       public Greeting() {
          // empty
       }
    
       public Greeting(String newGreeting) {
          greetingText = newGreeting;
       }
    
       public String hello(String msg) {
          String helloMsg = greetingText + " " + msg;
          messageCache.add(helloMsg);
          return helloMsg;
       }
    
       public String toString() {
          return messageCache.toString();
       }
    }

    This simple bean has one method, hello(String), that;

    1. Attaches a greeting string to the beginning of the method argument.
    2. Caches the modified string.
    3. Returns the modified string.

    The hello(String) method provides a way to customize the greeting in the constructor and to retrieve the cache. A Camel route can use these various points of entry into the bean code.

    Step 4: Create a Route

    Create a route that will use the bean:

    1. In the route editor, click the Design tab.
    2. In the Palette, under Components, click and drag Direct onto the Route_route1 container. This tutorial uses the Direct component to keep the example simple. You can, of course, use a File component, JMS component, or some other component.
    3. With the new “direct” component in the route selected:
      1. Open the Properties view.
      2. Click the Details tab.
      3. In the Uri field, change the value to direct:beanTutorial.
    4. In the Palette, under Components, click and drag the Bean component onto the direct component.
    5. With the Bean_bean1 component selected, in the Properties view:
      1. Select the Details tab.
      2. In the Ref field, display the dropdown list.
      3. Select the GreetingBean, which appears here because you previously added it as a global bean. This creates a reference to the global bean and inserts it into the route.
    6. In the Palette, drag another Direct component onto the Bean in the route.
    7. With the second “direct” component in the route selected, in the Properties view:
      • In the Uri field, change the value to direct:beanTutorialOut. This creates the final destination for the route.
    8. Save the route.

    Step 5: Create a Test

    In the project you created in step 1, create a test:

    1. In the Project Explorer view, in the src folder, create a new folder named test.
    2. In the test folder, create a new folder named java.
    3. In the Developer Studio menu bar, select File -> New -> Camel Test Case.
    4. In the New Camel JUnit Test Case wizard:
      1. Make sure the Source Folder path is global-bean-tutorial/src/test/java.
      2. In the Package field, enter org.example.
      3. To the right of the Camel XML file under test field, click Browse.
      4. Select the blueprint.xml file and click OK.
      5. In the Name field, enter GreetingTest.
      6. Click Finish.

    Step 6: Finish the Generated Test

    The Camel Test Case wizard generates a test class that extends CamelBlueprintTestSupport and provides some templated code. To finish the generated test, provide some sample input and output messages and then send them to the project’s Camel route.

    Change your class to resemble the following:

    package org.example;
    
    import org.apache.camel.EndpointInject;
    import org.apache.camel.Produce;
    import org.apache.camel.ProducerTemplate;
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.component.mock.MockEndpoint;
    import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
    import org.junit.Test;
    
    public class GreetingTest extends CamelBlueprintTestSupport {
    
      // Input message bodies to test with
      protected Object[] inputBodies = 
        { "This is one bean example.",
          "This is another bean example." };
    
      // Expected message bodies
      protected Object[] expectedBodies = 
        { "Hello This is one bean example.",
          "Hello This is another bean example."};
    
      // Templates to send to input endpoints
      @Produce(uri = "direct:beanTutorial")
      protected ProducerTemplate inputEndpoint;
    
      // Mock endpoints used to consume messages from the output 
      // endpoints and then perform assertions
      @EndpointInject(uri = "mock:output")
      protected MockEndpoint outputEndpoint;
    
      @Test
      public void testCamelRoute() throws Exception {
         // Create routes from the output endpoints to our mock 
         // endpoints so we can assert expectations
         context.addRoutes(new RouteBuilder() {
              @Override
              public void configure() throws Exception {
                   from("direct:beanTutorialOut").to(outputEndpoint);
              }
         });
    
         // Define some expectations
         outputEndpoint.expectedBodiesReceivedInAnyOrder(expectedBodies);
    
         // Send some messages to input endpoints
         for (Object inputBody : inputBodies) {
              inputEndpoint.sendBody(inputBody);
         }
    
         // Validate our expectations
         assertMockEndpointsSatisfied();
      }
    
      @Override
      protected String getBlueprintDescriptor() {
         return "OSGI-INF/blueprint/blueprint.xml";
      }
    }

    With these changes in place, in the Project Explorer view, right-click the GreetingTest class and select Run As -> JUnit Test. Your test should pass.

    The next route editor bean tutorial article in this series will show how to:

    • Update the route to be more specific about which bean method to call.
    • Customize the greeting by passing a constructor argument.
    • Update the test according to the route and bean updates.

    Click here to for an overview of Red Hat JBoss Fuse a lightweight and modular integration platform.

    Last updated: November 9, 2023

    Recent Posts

    • Assessing AI for OpenShift operations: Advanced configurations

    • OpenShift Lightspeed: Assessing AI for OpenShift operations

    • OpenShift Data Foundation and HashiCorp Vault securing data

    • Axolotl meets LLM Compressor: Fast, sparse, open

    • What’s new for developers in Red Hat OpenShift 4.19

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue