3

I have WAR package with CDI beans. Deployment of the package is very slow because every time during deployment the package is scanned for CDI beans. Is there any option to disable this process?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

6

The correct way is to disable discovery in the beans.xml of the relevant archive:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                        http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    version="1.1" bean-discovery-mode="none">
</beans>

According to the CDI specification this removes the archive from the list of bean-archives.

Steven Pessall
  • 983
  • 5
  • 15
  • Well I get java.lang.IllegalStateException: Singleton not set for STATIC_INSTANCE => [] – Peter Penzov Jun 16 '15 at 10:44
  • Are you surce you can disable CDI for your archive? – Steven Pessall Jun 16 '15 at 10:53
  • I want to speed up the package deployment by disabling the scan of the package by the CDI framework, not to disable the usage during run time. – Peter Penzov Jun 16 '15 at 10:56
  • And how should CDI know which Managed Beans are present without scanning the archives? You might be able to speed up the deployment by splitting up your software into multiple jars and only placing a beans.xml in some of them. But that is another question altogether. – Steven Pessall Jun 16 '15 at 11:02
2

I have a feeling that what you're looking for is more of a tool. As mentioned, Weld uses class scanning to find annotations. There are ways to speed this up. One that works pretty well is Jandex, an annotation processor that can be used at compile time to create an index (easier to read database) of your classes and annotations. This does dramatically boost deployment times.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • Interesting suggestion. But it seems like Weld 2.2 already uses Jandex ([link](https://issues.jboss.org/browse/WELD-1588)). – Steven Pessall Jun 17 '15 at 07:39
  • Yes, weld 2.2 uses Jandex. However in order for your app to use Jandex you need to enable the plugin at compile time. – John Ament Jun 17 '15 at 10:38
  • Ah, okay. I have to try that out sometime. – Steven Pessall Jun 17 '15 at 14:23
  • Hi John, is there something more to be done to enable Jandex to get used. I have just tried deploying a quite big application with and without Jandex. The WAR file with jandex is about 2MBs or so bigger for a total war size of 79 MBs against a war without any jandex information that is 77 MB. the Deployment times are unaffected at all. If anything the WAR with jandex seems to take 2 seconds longer on the last test I did. So clearly, I felt no relevant benefit on having the index in the WEB-INF/lib/jar files. I am not sure if I am doing something wrong... but there is no improvemnet. – 99Sono Aug 29 '17 at 09:35
  • Does the fact that the JAR files contain a beans.xml with discover-mode=all play a role? Does htis somehow given an indication to WELD not to use jandex and just do the normal scanning? To me it makes no sense... some degree of improvement should have been felt. – 99Sono Aug 29 '17 at 09:37
  • Hello, this is a very old question. I would recommend you starting your own question @99Sono – John Ament Aug 29 '17 at 13:48
  • Done. https://stackoverflow.com/questions/45995249/cdi-and-jandex-and-deployment-enhancement-approach-no-speed-difference-detecte – 99Sono Sep 01 '17 at 09:29