0 x00 the
This article is to share with you some concepts related to Java through the compilation process of an open source project.
0 x01 reason
Recently, I was compiling Sofa – Registry of Ant Financial. Due to unknown reasons, I could not completely download the dependent Maven packages, so I had to manually download them one by one. This proved to be a painful process, as the various Java packages were interlinked and java-related concepts sprang to mind. Just sort out these concepts one by one and share them with you.
For example, from the output of MVN Dependency: Tree, we encountered concepts or nouns like: Glassfish, Javax.ws. Rs, Jersey, Jetty, HK2, Javax.Inject, javax.annotation……
[INFO] +- com.alipay.sofa:registry-remoting-http:jar:5.42.:compile
[INFO] | +- org.glassfish.jersey.core:jersey-server:jar:2.26:compile
[INFO] | | +- org.glassfish.jersey.core:jersey-common:jar:2.26:compile
[INFO] | | | \- org.glassfish.hk2:osgi-resource-locator:jar:1.01.:compile
[INFO] | | +- javax.ws.rs:javax.ws.rs-api:jar:2.1:compile
[INFO] | | +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.251.:compile
[INFO] | | | \- org.glassfish.hk2:hk2-api:jar:2.5. 0-b32:compile
[INFO] | | +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] | | \- org.glassfish.hk2.external:javax.inject:jar:2.5. 0-b42:compile
[INFO] | +- org.glassfish.jersey.inject:jersey-hk2:jar:2.26:compile
[INFO] | | \- org.glassfish.hk2:hk2-locator:jar:2.5. 0-b42:compile
[INFO] | | +- org.glassfish.hk2:hk2-utils:jar:2.5. 0-b42:compile
[INFO] | | | \- javax.inject:javax.inject:jar:1:compile
[INFO] | | \- org.javassist:javassist:jar:3.21. 0-GA:compile
[INFO] | +- org.eclipse.jetty:jetty-server:jar:9.419..v20190610:compile
Copy the code
0 x02 concept
2.1 the JSR
JSR stands for Java Specification Requests, which stands for Java Specification proposals. Is a formal request to the JCP (Java Community Process) to add a standardized technical specification. Anyone can submit a JSR to add new apis and services to the Java platform. JSR has become an important standard in the Java world.
2.2 javax.mail
Java and Javax are both Java Application Programming Interface (API) packages. Java is the core package, and Javax’s X stands for extension.
The Java class library is the base library that was established when Java was released, and the Javax class library is an added layer on top of that to keep the original for version compatibility, but some things have a better solution, so add some. Typical examples are AWT (Abstract Windowing ToolKit) and Swing.
2.3 JSR311
2.3.1 JSR311
JSR311 is an API specification for implementing Restful Web Services in Java (JSR311: JAX-RS: The Java API for Restful Web Services).
JSR311 has an important goal: to expose POJOs as Web services using annotations, making them lightweight.
- Jsr311-api – This is the official specification JAR for jax-RS 1.x series
- Javax.ws. Rs-api – This is the official specification JAR for jax-RS 2.x series
2.3.2 javax.mail. Ws. Rs
Java.ws. Rs is the package name defined in the JAX-RS specification.
Jax-rs stands for Java API for RESTful Services. The current version of the specification is 2.0.
Jax-rs defines:
A set of boot modes
(JEE as HTTP container or servlet as HTTP container)A set of annotations
@GET, @POST, @DELETE, @PUT, @Consumes ...
Provide Rest services through POJO Resource classes
Just like the JSR specification defines servlets to inherit HttpServlet and rewrite doGet, doPost, do… Same method. Any of us who follow this standard can call it a Servlet program. You can write Servlet programs that run unmodified in any implementation Servlet container. Similarly, you can write JAX-RS programs that work with any JAX-RS framework without modification.
Spring MVC uses servlets as HTTP containers and builds its own Api that does not follow the JAX-RS specification.
2.3.3 framework
There are many frameworks that implement the JAX-RS standard:
- Apache CXF, an open source Web services framework.
- Jersey, a reference implementation of JAX-RS provided by Sun.
- RESTEasy, implementation of JBoss.
- Restlet, developed by Jerome Louvel and Dave Pawson, is the earliest REST framework, predating JAX-RS.
- Apache Wink, a project in the Apache Software Foundation incubator, implements the JAX-RS specification for its service modules
2.3.4 Jersey
Jersey is a JAX-RS (JSR311) open source reference implementation.
SpringMVC does not support the JSR311/JSR339 standard when developing REST applications. If you want to act in accordance with standards, the most common frameworks that implement these two standards are Jersey and CxF. But because Jersey was the original implementation and the primary object of reference for JSR311, it can be said that Jersey is the de facto standard (similar to Hibernate being the de facto standard for JPA) and is one of the most widely used REST development frameworks today.
Jersey is used to build RESTful Web services. Jersey also provides additional apis and extension mechanisms, so developers can extend Jersey as they see fit.
Sun. Jersey and Glassfish. Jersey are two versions of Jersey, corresponding to 1.x and 2.x, where:
- 1. The Jersey package in x starts with com.sun.
- 2. X is prefixed with org.glassfish.
2.4 the JSR – 330
Against 2.4.1 JSR – 330
Jsr-330 is the Java dependency injection standard. The following terms are defined to describe dependency injection:
- A depends on B (or B is depended on by A). A is called dependency.
- The procedure for finding a dependency at run time is called a parsing resolving dependency
- If no instance of a dependency can be found, the dependency is called “unsatisfied”
- In dependency injection dependency Injection, the tool that provides dependencies is called dependency Injector Dependency Injection
Javax.mail 2.4.2. Inject
The use of dependency injection is defined, but the implementation and configuration are not. The Java EE package javax. Inject corresponds to this standard. It also defines only the use of dependency injection (that is, through annotations), and also does not define how dependency injection is configured or implemented.
Javax. inject provides the following five annotations (Inject, Qualifier, Named, Scope, Singleton) and one interface (Provider).
2.4.3 framework
There are many frameworks that support JSR-330:
- Dagger2 under Android is based on this specification. Jsr-330 standard annotations in Dagger2 include: @inject @qualifier @scope @named, etc.
Guice
DI is a popular, lightweight DI framework implemented by Google for Java 6 and above.- Other injection frameworks such as Spring also support JSR-330.
It is also important to understand the differences between jSR-330 annotations and Spring annotations when using them, as shown in the table below.
Spring | javax.inject.* | Javax.mail. Inject limit |
---|---|---|
@Autowired | @Inject | @Inject Annotations withoutrequired Property, but is available via Java 8Optional replace |
@Component | @Named | The JSR_330 standard does not provide a composite model, only one way to identify components |
@ the Scope (” singleton “) | @Singleton | Jsr-330’s default scope is similar to Spring’sprototype However, in keeping with Spring’s default, beans in the JSR-330 standard are singleton by default in Spring. To use scopes that are not singletons, developers should use Spring’s@Scope Annotation.java.inject Also provide a@Scope Annotations, however, can only be used to create custom scopes. |
@Qualifier | @Qualifier/@Named | javax.inject.Qualifier Just a meta-annotation to build custom qualifiers. String qualifiers (as in Spring@Qualifier ) can passjavax.inject.Named To implement the |
@Value | – | inequitable |
@Required | – | inequitable |
@Lazy | – | inequitable |
ObjectFactory | Provider | javax.inject.Provider Is SpringObjectFactory The other option is to passget() Method to proxy,Provider You can use Spring’s@Autowired Use a combination of |
2.4.4 hk2
HK2 is a lightweight dynamic dependency injection framework that is an implementation of JSR-330.
HK2 stands for Hundred Kilobytes Kernel, including Modules Subsytem and Component Model. SUN implemented HK2 as its system kernel in its open source GlassFish J2EE application server project.
In the HK2 component model, the functionality of a component is declared through the service interface – service implementation pattern. An HK2 service interface identifies and describes a building block or application extension point. The HK2 service implementation implements the HK2 service interface.
Hk2 package is org.glassfish.hk2.
2.5 the JSR 250
2.5.1 JSR 250
The JSR 250 specification contains annotations for injecting resources into endpoint implementation classes and annotations for managing the application lifecycle.
2.5.2 javax.mail. The annotation
The Java™ class that contains each annotation in the JST 250 standard is named javax.annotation.xxx, where XXX is the name of the annotation following the “@” character. @ the Resource, for example, annotations Java class called javax.mail. The annotation. The Resource.
Javax. annotation contains the following annotations:
- @generated: Generates an annotation for a resource. The instance Generated by this tag is a resource. Similar to the @bean annotation in Spring, used to generate a conventional resource.
- PostConstruct handles the callback after creating the resource.
- @predestroy Callback processing before resource destruction.
- The @resource flag indicates where the Resource is used. Functions are similar to @Autowired and @Inject, but there are many differences between them.
- The @Resources tag uses the location of multiple Resources, similar to using @AutoWired to load data into a list.
2.5.3 framework
If you look closely at these annotations in the JSR-250 definition, you’ll see that they are all about the building, destruction, and use of “resources.”
Spring implements @PostConstruct, @PreDestroy, and @Resource.
2.6 the Jakarta
Oracle decided to hand over JavaEE to the Eclipse Foundation, an open source organization, but did not want JavaEE to continue to use the Java name. Eclipse did a poll, and JakartaEE won by a clear margin. Eclipse therefore announces that it is officially changing the name of JavaEE to JakartaEE.
The Eclipse Foundation has also renamed each specification of the Java EE standard, clarifying each specification’s future role in the Jakarta EE platform.
The new name Jakarta EE is the second rename of Java EE. In May 2006, the term “J2EE” was dropped in favor of the name Java EE. The number 2 was dropped from YouTube’s name when it was still an independent company, and Pluto was still considered a planet. Similarly, the number 2 was removed from J2SE as part of Java SE 5 (2004), before Google went public.
Jakarta EE provides a very clear dividing line because the Javax namespace can no longer be used.
- Jakarta 9 (2019 and later) uses the Jakarta namespace.
- Java EE 5 (2005) through Java EE 8 (2017) use javax namespaces.
- Java EE 4 uses the Javax namespace.
When it comes to Java rebranding, I’m reminded of the tragic decline of the JavaEye website when it changed its name to ItEye.
2.7 anyone
The Eclipse Foundation does more than just publish specifications. It also released Eclipse GlassFish 5.1, a ready-to-run implementation of Jakarta EE 8. It is also certified as an open source compatible implementation of the Jakarta EE 8 platform.
GlassFish is the name of the open source development project used to build Java EE 5 application servers. It is based on source code from Sun Java System Application Server PE 9 provided by Sun Microsystems and TopLink persistence code contributed by Oracle. The project provides a structured process for developing high-quality application servers that deliver new functionality at an unprecedented rate. This is in response to Java developers who want to get their hands on the source code and contribute to Sun’s next generation application server (based on GlassFish). The project is designed to facilitate communication between Sun and Oracle engineers and the community. It will enable all developers to participate in the application server development process.
In the past, the Process by which new EE functionality was born was called the Java Community Process.
Java SE still uses JCP today. However, since EE has changed its ownership from Oracle to Eclipse Foundation, we have a new standalone process. It is the Eclipse Foundation Specification Process (EFSP), an extension of the Eclipse Development Process.
As part of JCP, JSR requires a concrete reference implementation. It’s kind of like a class that implements an interface. Reference implementations must be compatible with previous library packages or with developers from other organizations creating their own specification implementations.
For Java EE functionality, JCP uses Glassfish as its reference implementation.
2.8 Jetty
Jetty is an open source servlet container that provides a runtime environment for Java-based Web containers such as JSPS and servlets. Jetty is written in the Java language, and its API is distributed as a set of JAR packages. Developers can instantiate the Jetty container as an object that can quickly provide network and Web connectivity for some standalone Java applications. Due to its lightweight and flexible nature, Jetty is also used in some well-known products, such as ActiveMQ, Maven, Spark, GoogleAppEngine, Eclipse, Hadoop, etc.
Why Jetty?
- Asynchronous servlets support higher concurrency
- Modular design, more flexible, easier to customize, also means higher resource utilization
- Jetty’s default NIO model is a better choice in business scenarios with a large number of long connections
- Embedding Jetty into an application enables a normal application to quickly support HTTP services
2.9 Conceptual Relationships
Among the concepts involved above, several relationships are as follows (only a rough logical diagram, not representative of inheritance, etc.) :
+--------+ +--------+ +---------+ Jakarta
| JSR311 | | JSR330 | | JSR 250| +----+---+ +----+---+ +----+----+ | | | v v v +------+-----+ +------+-----+ +--------+-------+ |javax.ws.rs | |javax.inject| |javax.annotation| +------+-----+ +------+-----+ +--------+-------+ | | | | v | | +-------+---------+ | | +-----------+ |org.glassfish.hk2| | | | +-----------------+ | | | | v v | +------+-----+-------+ | |org.glassfish.jersey| <----------------------------------------+ +-------------------++ | v +---+---+ | Jetty | +-------+Copy the code
0x03 use in SOFARegistry
Let’s look at how some of the concepts mentioned earlier are used in SOFARegistry.
3.1 javax.mail. Ws. Rs
Javax.ws. Rs is the package name of JSR311. An important goal is to expose POJOs as Web Services using annotations.
As an example, we can see that DecisionModeResource has been exposed as a Web Service:
package com.alipay.sofa.registry.server.meta.resource;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("decisionMode")
public class DecisionModeResource {
@Autowired
private MetaServerConfig metaServerConfig;
@POST
@Produces(MediaType.APPLICATION_JSON)
public Result changeDecisionMode(DecisionMode decisionMode) {
((MetaServerConfigBean) metaServerConfig).setDecisionMode(decisionMode);
Result result = new Result();
result.setSuccess(true);
returnresult; }}Copy the code
3.2 the jersey and jetty
Because of the characteristics of the jetty lightweight, use the org. Eclipse in SOFARegistry jetty. Server. The server, thus opening a drama.
com.alipay.sofa.registry.remoting.jersey.JerseyJettyServer
import javax.ws.rs.ProcessingException;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.glassfish.jersey.jetty.JettyHttpContainer;
import org.glassfish.jersey.jetty.internal.LocalizationMessages;
import org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler;
import org.glassfish.jersey.server.ContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.Container;
public class JerseyJettyServer implements Server {
private org.eclipse.jetty.server.Server server;
public static org.eclipse.jetty.server.Server createServer(final URI uri,
final ResourceConfig resourceConfig,
final boolean start) {
JettyHttpContainer handler =
ContainerFactory.createContainer(JettyHttpContainer.class, resourceConfig);
final org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(
newJettyConnectorThreadPool()); server.setHandler(handler); server.start(); }}Copy the code
3.3 hk2
HK2 is a lightweight dynamic dependency injection framework that is an implementation of JSR-330. Its use is widespread and low-level, with various direct and indirect uses in Jersey, for example.
packageorg.glassfish.jersey.server; .import org.glassfish.jersey.internal.inject.Binder;
import org.glassfish.jersey.internal.inject.Bindings;
import org.glassfish.jersey.internal.inject.CompositeBinder;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Injections;
import org.glassfish.jersey.internal.inject.InstanceBinding;
importorg.glassfish.jersey.internal.inject.Providers; .public final class ApplicationHandler implements ContainerLifecycleListener {...public ApplicationHandler(final Class<? extends Application> jaxrsApplicationClass) {
initialize(new ApplicationConfigurator(jaxrsApplicationClass), Injections.createInjectionManager(), null); }... }Copy the code
Org. Anyone. Jersey. Internal. Inject. * is an encapsulation of hk2, select a stack for everyone to see, to see the final call to hk2.
value:73, NamedImpl (org.glassfish.hk2.utilities)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
invoke:309, AnnotationLiteral (org.glassfish.hk2.api)
hashCode:242, AnnotationLiteral (org.glassfish.hk2.api)
hash:339, HashMap (java.util)
put:612, HashMap (java.util)
add:220, HashSet (java.util)
getThreeThirtyDescriptor:1282, Utilities (org.jvnet.hk2.internal)
initialize:76, ServiceLocatorGeneratorImpl (org.jvnet.hk2.external.generator)
create:103, ServiceLocatorGeneratorImpl (org.jvnet.hk2.external.generator)
internalCreate:312, ServiceLocatorFactoryImpl (org.glassfish.hk2.internal)
create:268, ServiceLocatorFactoryImpl (org.glassfish.hk2.internal)
createLocator:114, AbstractHk2InjectionManager (org.glassfish.jersey.inject.hk2)
<init>:86, AbstractHk2InjectionManager (org.glassfish.jersey.inject.hk2)
<init>:62, ImmediateHk2InjectionManager (org.glassfish.jersey.inject.hk2)
createInjectionManager:79, Hk2InjectionManagerFactory$Hk2InjectionManagerStrategy$1 (org.glassfish.jersey.inject.hk2)
create:97, Hk2InjectionManagerFactory (org.glassfish.jersey.inject.hk2)
createInjectionManager:93, Injections (org.glassfish.jersey.internal.inject)
<init>:282, ApplicationHandler (org.glassfish.jersey.server)
<init>:469, JettyHttpContainer (org.glassfish.jersey.jetty)
createContainer:61, JettyHttpContainerProvider (org.glassfish.jersey.jetty)
createContainer:82, ContainerFactory (org.glassfish.jersey.server)
createServer:100, JerseyJettyServer (com.alipay.sofa.registry.remoting.jersey)
startServer:83, JerseyJettyServer (com.alipay.sofa.registry.remoting.jersey)
......
Copy the code
0xEE Personal information
★★★★ Thoughts on life and technology ★★★★★
Wechat official account: Rosie’s Thoughts
If you want to get a timely news feed of personal articles, or want to see the technical information of personal recommendations, please pay attention.
0 XFF reference
Installation and use of Glassfish
What is Jersey a framework and what is its value?
jetty
javax
What is the difference between import Java and Javax?
jakarta.apache.org/
JEE, J2EE and Jakarta concepts explained
No more JavaEE, now called JakartaEE
Jersey development RESTful (seven) Jersey quick start
Javax.mail. Ws. Rs restful development
Introduction to the Java dependency injection standard JSR-330
Java EE is Jakarta EE again
Preliminary Study on Lightweight IOC/DI Capacity HK2 (Zero Configuration)
JSR311 makes Restful WebService simple
Spring-jsr-330 standard annotations