[JAVA] [Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI

Overview

Making CDI available in Apache Tomcat is tedious by writing and configuring pom.xml. So, this time, I will use Apache OpenWebBeans to make it easy to use CDI.

Prerequisites

  1. Java command can be used
  2. Git can be used
  3. Can use eclipse (if possible)

overall structure

/Work destination/
  ├ Tomcat/
  │  ├ apache-tomcat-9.0.22/
  │  │  ├ bin/
  │  │  │  ├ startup.bat
  │  │  │  └ startup.sh
  │  │  └ conf/
  │  │     ├ Catalina/
  │  │     │  └ localhost
  │  │     │     └ Sample.xml
  │  │     ├ context.xml
  │  │     └ logging.properties
  │  ├ current -> (/Work destination/Tomcat/apache-tomcat-9.0.22/)
  │  ├ openwebbeans-distribution-2.0.9
  │  │  ├ install_owb_tomcat7.bat
  │  │  └ install_owb_tomcat7.sh
  │  ├ apache-tomcat-9.0.22
  │  └ openwebbeans-distribution-2.0.9-binary.zip
  └ quita
     └ cdi-sample
        └ WebContent
          └ WEB-INF
            └ beanse.xml

Work procedure

  1. Create a directory for this work. (Hereafter, work destination)

  2. Create a Tomcat directory at your destination.

  3. Download Apache Tomcat to the Tomcat directory and unzip it.

  4. Create a current link in consideration of the version change.

    #For Windows
    mklink /j current apache-tomcat-9.0.22
    
    #For linux
    ln -s apache-tomcat-9.0.22 ./current
    
  5. [Implemented for Windows] Change conf / logging.properties in Apache Tomcat to prevent the console from being garbled.

    #Near line 51
    #Change before
    java.util.logging.ConsoleHandler.encoding = UTF-8
    #After change
    java.util.logging.ConsoleHandler.encoding = SJIS
    
  6. Download Apache OpenWebBeans to the Tomcat directory and unzip it.

  7. Run install_owb_tomcat7 in Apache OpenWebBeans

    #For Windows
    install_owb_tomcat7.bat ..\current
    
    #For linux
    install_owb_tomcat7.sh ../current
    
  8. [Implemented for Windows] Add a tag in the "" tag of conf / context.xml in Apache Tomcat, and do as follows.

    <Context>
    
        <!-- Default set of monitored resources. If one of these changes, the    -->
        <!-- web application will be reloaded.                                   -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
        <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
        <!--↓ Added tag-->
        <Listener className="org.apache.webbeans.web.tomcat7.ContextLifecycleListener"/>
        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->
    </Context>
    
  9. Clone the following Git to your destination

    • https://gitlab.com/P488/qiita.git
  10. Create xml with the following contents in conf / Catalina / localhost in Apache Tomcat.

``` xml:Sample.xml
<Context path="/Sample" reloadable="true" docBase="/Work destination/quita/cdi-sample/WebContent">
</Context>
```
  1. Run startup in the Apache Tomcat bin
``` sh
#For Windows
startup.bat

#For linux
startup.sh
```
  1. Access localhost: 8080 / Sample / SampleServlet and confirm that "Served at:" is output.

Impressions / Summary

Now I've used Apache OpenWebBeans to enable Apache Tomcat to use CDI. In the case of Windows, it is a little annoying because it requires manual work, but I wondered if it would be easy to use CDI by executing bat / sh. The source used this time is created with eclipse, so if you can, you may want to play around with it. (When using with eclipse, change the Tomcat installation directory to / work / Tomcat / current from Window-> Settings-> Server-> Runtime environment-> Tomcat9 (Java11)-> Edit)

Please comment if you have any mistakes or problems.

Recommended Posts

[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI
How to download the old version of Apache Tomcat
The story of introducing Ajax communication to ruby
The story of raising Spring Boot 1.5 series to 2.1 series
The story of adding the latest Node.js to DockerFile
The story of migrating from Paperclip to Active Storage
The first step to using Xib instead of StoryBoard
The story of making Dr. Oakid using LINE BOT
The story of raising Spring Boot from 1.5 series to 2.1 series part2
20190803_Java & k8s on Azure The story of going to the festival
The story of fighting ubuntu errors 0x80370114, 0x80000000 to enable docker desktop with wsl2 on windows10 home
The story of Collectors.groupingBy that I want to keep for posterity
How to get the contents of Map using for statement Memorandum
The story of toString () starting with passing an array to System.out.println
The story of making a communication type Othello game using Scala.
[Java version] The story of serialization
The story I wanted to unzip
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
I want to display the number of orders for today using datetime.
The story of forgetting to close a file in Java and failing
A story I was addicted to when testing the API using MockMVC
The story of switching from Amazon RDS for MySQL to Amazon Aurora Serverless
The story of releasing the Android app to the Play Store for the first time.
I measured the performance of the in-memory data grid Apache Ignite using Yardstick.
[Java] How to get to the front of a specific string using the String class
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Ruby On Rails] How to search the contents of params using include?
The story of encountering Spring custom annotation
Try using || instead of the ternary operator
The secret to the success of IntelliJ IDEA
Test the integrity of the aggregation using ArchUnit ②
The story of updating SonarQube's Docker Container
How to determine the number of parallels
The story of RxJava suffering from NoSuchElementException
The story of AppClip support in Anyca
How to sort the List of SelectItem
Using SSIServlet with built-in Tomcat of SpringBoot 2.2.4
Output of the book "Introduction to Java"
Create RestAPI using wicketstuff-restannotations of Apache Wicket
The story of writing Java in Emacs
The process of introducing Vuetify to Rails
Test the integrity of the aggregation using ArchUnit ③
The story of intentionally using try catch for the first time in my life
I want to get the value of Cell transparently regardless of CellType (Apache POI)
Volume of optimizing Hyper-v to eliminate the tightness of disk capacity (engineer using Docker)
Up to the point of launching a Docker container built using RedHat Quarkus
[Java] How to easily get the longest character string of ArrayList using stream
Pay attention to the boundary check of the input value when using the float type
Send a notification to slack with the free version of sentry (using lambda)