Make war with with embedded tomcat - java

I have recently created a web application using spring mvc, gradle and tomcat to host it. I have been trying to create a war file of the web application which can be executed on its own with no need to have gradle and tomcat installed on your computer.
For example running java -jar <path-to-war> and the server will be running on the localhost port specified.
What is the best way to approach this?

use spring boot, comes with embedded tomcat,include below in pom and
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Main method
#SpringBootApplication
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
}

Related

how to make keycloak work with embedded jetty?

Hello every one I have just been trying out key-cloak for IAM and it seems a great tool to me but one thing that I can't seem to figure out is how to integrate it with my current JAX-rs web services that run on an embedded jetty container. As far as I have searched no key-cloak client adapters exist for embedded jetty server and the jetty 9.x adapters only seem to work for jetty standalone (non-embedded) server. Does this mean I have to implement my own client adapter by implementing ContaineRequestFilter class and analyzing the request headers and utilizing the token introspection endpoint for resource server authorization? Also is this how all other client adapters work or is it some other way?
There are two interesting projects on github, which both use Spring Boot:
https://github.com/Baeldung/spring-security-oauth/tree/master/oauth-sso/sso-authorization-server
Documented here: https://www.baeldung.com/keycloak-embedded-in-spring-boot-app
If you prefer Jetty as embedded webserver, change the pom.xml:
Exclude tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Add Jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>provided</scope>
</dependency>
Also this project is interesting: https://github.com/thomasdarimont/embedded-spring-boot-keycloak-server

Am i using the Tomcat webserver or not?

so i'm making this application in Spring Boot and i know by default, spring boot is embedded to tomcat web server. But i'm not sure whether it's tomcat web server or else because in my dependency there is no tomcat dependency in my code. but i access the webserver succesfully through
localhost:8080
and here is my dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
so am i using the tomcat server or not?
You're using embedded tomcat by default as spring-boot-starter-web has a dependency on tomcat libraries which are transitive.
To be more precise, spring-boot-starter-web among other things has the following definition in its pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
Check your logs, please, or console messages,
Spring Boot prints this
2018-09-25 00:45:24 INFO TomcatWebServer 91: Tomcat initialized with port(s): 8080 (http)
spring-boot-starter-web comes with Embedded Tomcat. In case you want to edit the
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
It has the below properties :
server:
port:
undertow:
ioThreads:
workerThreads:
accesslog:
enabled:
compression:
enabled:
mimeTypes:
minResponseSize:

Integrate Apache Mahout with ElasticSearch in Spring Boot

I have a Spring Boot application integrated with ElasticSearch.
When i am trying to add Apache Mahout as a maven dependency, the application is no longer running with this error:
Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/lucene/util/Accountable
If i remove the mahout dependency the application is running.
pom.xml:
<dependencies>
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-core</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
The dependency is resolved by maven, the project compiles with no error but when i start it, the server stops.
Any idea?
Thank you
In order to use the latest versions of Elasticsearc v6.x and Spring-boot, you have to focus on the matrix versions. because while I was trying to integrate Elasticsearch v6.2.2 as dependencies into my spring-boot v1.5.X I faced several errors. thus spring-boot v2.x is the compatible one with ES v6.x
so the following link will help you to know the matrix between versions for your project :
https://www.elastic.co/support/matrix#matrix_compatibility
after doing that , use the last version of apache mahout .

Input Channel subscriber not up for Custom Sink in Spring Cloud Data flow

I was trying to deploy my own custom sink of spring cloud data flow onto cloud foundry.
My Dependency are below :
<dependencies>
<dependency>
<groupId>org.springframework.cloud.stream.app</groupId>
<artifactId>spring-cloud-starter-stream-sink-log</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
<version>1.2.0.RC1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud.stream.app</groupId>
<artifactId>app-starters-core-dependencies</artifactId>
<version>1.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud.stream.app</groupId>
<artifactId>log-app-dependencies</artifactId>
<version>1.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
And the custom code is very basic as below :
#EnableBinding(Sink.class)
public class SinkConfiguration {
private static final Log logger = LogFactory.getLog(SinkConfiguration.class);
#ServiceActivator(inputChannel=Sink.INPUT)
public void loggerSink(String payload) {
logger.info("Hello, Rahul. The time is: " + payload);
}
}
All I see when i deploy this application is that the Error channel subscriber is created , but no Input subscriber was created. Due to this no messages are being received on to this app. Source for this app is a custom source with rest controller. The default out of box streamer app -- LogSink works successfully. But i need to create a customsink to build things on top. Do anyone see an issue what I am missing here?
If your goal is to create an entirely new custom sink, you would develop that as a standalone application by following the Spring Initializr procedure. It is simple this way and you don't need to use the existing log-sink starter in this case.
If you are trying to patch any of the OOTB application; in this case, the log-sink, then follow the patching procedure. Pay attention to importing the configuration class. Unless you do that, the associated app-starter's behavior won't kick in.
Also, it seems you're using an old release for rabbit-binder. It is better to rely on the Spring Initializr generated artifact as opposed to handcrafting dependency versions.

Spring Cloud Zuul CircuitBreaker All Routes via TurbineStream Not Turbine-AMQP

I'm using spring boot 1.3.1 and spring cloudl Brixtom.M4, While using springboot 1.3.1 i found that Turbine-AMQP project is no longer available instead we have now Spring Turbine Stream project.
I what to user SpringTurbine with rabbitmq or kafka and want to monitor hystrix stream of all routes registered in Zuul, I'm able to see the hystrix.stream for the zuul and also able to see that in hystrix dashboard, but not sure how to use the spring turbine stream.
On the net i found the code and documentation for using Turbine AMQP.
I have zuul server running ad http://localhost:9003/ with depedencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
and main.java as
#SpringBootApplication
#EnableZuulProxy
#EnableCircuitBreaker
public class EdgeServerApplication {
public static void main(String[] args) {
SpringApplication.run(EdgeServerApplication.class, args);
}
}
I also have springTurbinestream project as
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
spring-boot-starter-actuator
and main class for TurbineStream as
#SpringBootApplication
#EnableTurbineStream
#EnableDiscoveryClient
public class WiziqTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(WiziqTurbineApplication.class, args);
}
}
When I run the application and go to http://localhost:9003/hystrix.stream i see the stream but if i go to http://localhost:9003/turbine.stream it going on error.
What I'm doing wrong?
Your client app (on port 9003) is not supposed to have a /turbine.stream. It is supposed to send messages with hystrix metrics to rabbit (for instance). To make it do that you need to add spring-cloud-netflix-hystrix-stream and spring-cloud-starter-stream-rabbit (just like you did on the server for the *-turbine-* dependencies).

Categories