multi module maven project module expose as soap web service - spring

I have a multi module maven-spring project. Following is the structure-
ParentService
---ChildService-service
---ChildService-core
---ChildService-web
---ChildService-wsClient
---ChildService-mongo
I have created a new module called ChildService-wsService where I will write methods and expose as Axis2 SOAP web service. I have been able to write independent methods in classes of this module project and expose as service but I want to call methods of ChildService-service module.
When I try to call methods of ChildService-service module it gives me errors like NoClassDefFoundError.
Following is sample code--
public class HelloWorld {
#Autowired
private ITestService iTestService;
#Autowired
ICommonService commonService;
public String getVal(String s){
return s+"...testing...";
}
public String getValFfmService(){
iTestService=new TestServiceImpl();
return iTestService.test();
}
I am getting error as following--
Error: java.lang.NoClassDefFoundError: com/service/test/business/ITestService at java.lang.Class.forName0
If I include following line in class then also I am getting classnotfound error.
extends SpringBeanAutowiringSupport

You need to include your other projects that you need as dependencies, for instance, in your pom.xml:
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ChildService-service</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
...
</dependencies>
Then you need to install these dependencies in your local repository (mvn clean install will do it), or, in Eclipse, you need to activate Workspace resolution (right click on your module, Maven > Enable Workspace Resolution).

Related

Run a Maven plugin when the build fails

I am using a plugin to send a Slack message through Maven. I am wondering if it's possible to use a plugin when the build failed so I get automatically notified about the failed build?
You could do that within Maven itself, through the EventSpy mechanism, built-in from Maven 3.0.2. At each step of the build, several events are raised by Maven itself, or by custom code, and it is possible to listen to those events to perform some actions. The execution event raised by Maven are represented by the class ExecutionEvent. Each event has a type, that describes what kind of event it represents: project failure, Mojo failure, project skipped, etc. In this case, the project failure event is what you're looking for.
A custom spy on events is just a Java class that implements the EventSpy interface. Preferably, it should inherit from the AbstractEventSpy helper class. As an example, create a new project (let's call it my-spy), and add the following Java class under a package:
import org.apache.maven.eventspy.AbstractEventSpy;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.ExecutionEvent;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
#Component(role = EventSpy.class)
public class BuildFailureEventSpy extends AbstractEventSpy {
#Requirement
private Logger logger;
#Override
public void onEvent(Object event) throws Exception {
if (event instanceof ExecutionEvent) {
ExecutionEvent executionEvent = (ExecutionEvent) event;
if (executionEvent.getType() == ExecutionEvent.Type.ProjectFailed) {
logger.info("My spy detected a build failure, do the necessary here!");
}
}
}
}
This code simply registers the spy through the Plexus' #Component annotation, and logs a message when a project failed to build. To compile that class, you just need to add to the my-spy project a dependency on Maven Core and an execution of the plexus-component-metadata plugin to create the right Plexus metadata for the component.
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Once this project is compiled and installed into your local repository (through mvn clean install), you can add it to the build of another project through the core extensions mechanism.
Before Maven 3.3.1, you had to drop the my-spy JAR into your ${MAVEN_HOME}/lib/ext folder, so that Maven could find it. As of 3.3.1, you don't need to fiddle with your Maven installation, and can create a file .mvn/extensions.xml in your project base directory (${maven.multiModuleProjectDirectory}/.mvn/extensions.xml). Its content would be
<?xml version="1.0" encoding="UTF-8"?>
<extensions>
<extension>
<groupId>my.spy</groupId>
<artifactId>my-spy</artifactId>
<version>0.0.1</version>
</extension>
</extensions>
which just declares an extension pointing to the Maven coordinates of the spy project. Maven (≥ 3.3.1) will by default look for that file, and, as such, your spy will be correctly registered and invoked throughout the build.
The only remaining thing to do, is to code what the spy should do. In your case, it should invoke a Maven plugin, so you take a look at the Mojo Executor library, which makes that very easy to do.

error: cannot access BlockJUnit4ClassRunner

While i run the mvn install I'm able to find this above error .
This is my POM.xml i have configured JUnit.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
This is the service Test class
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public class ServiceTestCase {
protected static final Logger LOG = Logger.getLogger(ServiceTestCase.class);
#Configuration
static class AccountServiceTestContextConfiguration {
....
....
....
}
While compiling the above error I am getting.
this Test class I have created in src/test/java
can any one please suggest. How to resolve this ?
When I remove the I am getting error as #Test is not recognise.
Ok, the solution might be quite simple: Update to JUnit 4.5 (or higher).
The javadoc of the BlockJUnit4Runner (which is the Superclass of the SpringJUnit4ClassRunner you are using) states:
#since 4.5
...but as you only use <version>4.4</version>, that's probably the whole problem. Just checked it and the class does simply not exist in JUnit 4.4, so you'll have to upgrade your JUnit version to fix that problem.

Using Spring Boot together with gRPC and Protobuf

Anyone having any examples or thoughts using gRPC together with Spring Boot?
If it's still relevant for you, I've created gRPC spring-boot-starter here.
grpc-spring-boot-starter auto-configures and runs the embedded gRPC server with #GRpcService-enabled beans.
The simplest example :
#GRpcService(grpcServiceOuterClass = GreeterGrpc.class)
public static class GreeterService implements GreeterGrpc.Greeter {
#Override
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
// omitted
}
}
There is also an example of how to integrate the starter with Eureka in project's README file.
https://github.com/yidongnan/grpc-spring-boot-starter
In server
#GrpcService(GreeterGrpc.class)
public class GrpcServerService extends GreeterGrpc.GreeterImplBase {
#Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello =============> " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
In client
#GrpcClient("gRPC server name")
private Channel serverChannel;
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverChannel);
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
If you need a gRPC client library, i.e. consume stubs, check out my library https://github.com/sfcodes/grpc-client-spring-boot
This library will automatically scan your classpath, find all gRPC stub classes, instantiate them, and register them as beans with the ApplicationContext; allowing you to easily #Autowire and inject them just like you would any other Spring bean. For example:
#RestController
public class GreeterController {
#Autowired // <===== gRPC stub is autowired!
private GreeterGrpc.GreeterBlockingStub greeterStub;
#RequestMapping(value = "/sayhello")
public String sayHello(#RequestParam String name) {
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply reply = greeterStub.sayHello(request);
return reply.getMessage();
}
}
For gRPC server library, I'd also recommend LogNet/grpc-spring-boot-starter.
Starting from https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services, then
take a look at
SPR-13589 ProtobufHttpMessageConverter support for protobuf 3.0.0-beta4 and related SPR-13203
HttpMessageConverter based on Protostuff library
That is some support for proto3 is coming in Spring 5. As it is under development one is encouraged to vote and raise what is important for their project.
In here I use gRpc and eureka to communication. This project based on Spring-boot
https://github.com/WThamira/grpc-spring-boot
additionally you canuse register as consul also. full example in this repo
https://github.com/WThamira/gRpc-spring-boot-example
this maven dependency help to gRpc
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.0.1</version>
</dependency>
and need plugin show in below
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<!-- The version of protoc must match protobuf-java. If you don't depend
on protobuf-java directly, you will be transitively depending on the protobuf-java
version that grpc depends on. -->
<protocArtifact>com.google.protobuf:protoc:3.0.2:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
In this Github Repo[1] you will find an example of using gRPC to insert and view the users into the couchbase db. Please refer the proto file[2] to find the rpc methods.
Normally gRPC clients like bloomRPC is used to access the service. Using envoy proxy it is possible to transcode and access the service using HTTP/1.1. In the readme file the steps of creating a config file and to run the envoy proxy using docker file is shown.
[1] https://github.com/Senthuran100/grpc-User
[2] https://github.com/Senthuran100/grpc-User/blob/master/src/main/proto/user.proto
Created a simple Springboot App with GRPC. This GitHub repo has both Server and Client examples. Repo has separate Maven module(grpc-interface) where we declare the Proto files and generate the Java source code then can be used as lib in both Server and client apps.
https://github.com/vali7394/grpc-springboot-repo
you can use this page.
dependency and build tags was provided.
'https://www.baeldung.com/grpc-introduction'

Error: ClassCastException occured while adding dependency jar in pom.xml

Can any one suggest how to resolve the ClassCastException while adding dependency jar in pom.xml?
I have created a new maven project having returning the object in runtime and then execute the ovverride method definition as based upon client request.
Ex: actionPerform(actionType) -- this method return the 'Object'
public static CommonActions action(ActionTypes actionType)
{
return (CommonActions)actionPerform(actionType);
}
Code is working fine and then successfully executing. There is no ClassCastExceptions while adding this jar into library tab on project properties using eclipse.
Getting ClassCastExceptions on above return statement while adding the below depenedency in pom.xml
<dependency>
<groupId>com.amadeus.selenium.merci</groupId>
<artifactId>appium-amadeus-utilities</artifactId>
<version>1.4-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
I am using maven version 3.0.4
Please suggest how I resolve the ClassCastException at runtime while adding dependency jar in pom.xml.

How to add maven modules to IntelliJ?

IntelliJ cannot find the classes in a Maven module, even if I add the jar dependency into pom.xml.
I created a Maven module: test-intellij . And then I created a class TestApp and made it implements ApplicationContextAware as below:
public class TestApp implements ApplicationContextAware{
}
IntelliJ told me: "can not find class ApplicationContextAware".
So I pressed "alt + enter", then from the popup tips I chose "Add maven dependency".
After this operation, the dependency below was added into pom.xml successfully:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
But when I try to import the ApplicationContextAware class , IntelliJ still cannot find the ApplicationContextAware class to import.
Could anybody help me to solve this issue?
Try to Reimport the Maven project using the corresponding button in IntelliJ IDEA Maven Projects tool window. Wait until the dependency is downloaded and indexing is complete. File | Invalidate Caches may also help.
Also check that you are using the latest IDEA version (12.0.2 at the moment). You should be able to browse inside the downloaded dependency jar in the Project View, External Libraries node.

Resources