How do I initiate an infinite loop between the client and server, to send and receive POJO PingPong objects between the client and server?
When a connection is established, which seems to be happening, the client should send a PingPong, and the server should reply with a PingPong POJO as well. The client should (preferably) respond with another PingPong, creating an infinite loop.
the server receives an object:
BUILD SUCCESSFUL
Total time: 2 seconds
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x2cd55a8d] REGISTERED
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler bind
INFO: [id: 0x2cd55a8d] BIND(0.0.0.0/0.0.0.0:4454)
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x2cd55a8d, /0:0:0:0:0:0:0:0:4454] ACTIVE
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0x2cd55a8d, /0:0:0:0:0:0:0:0:4454] RECEIVED: [id: 0xe7c06459, /127.0.0.1:33182 => /127.0.0.1:4454]
^Cthufir#dur:~/NetBeansProjects/NettyServer$
thufir#dur:~/NetBeansProjects/NettyServer$
which was sent by the client:
BUILD SUCCESSFUL
Total time: 2 seconds
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x0f1fc251] REGISTERED
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler connect
INFO: [id: 0x0f1fc251] CONNECT(localhost/127.0.0.1:4454, null)
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x0f1fc251, /127.0.0.1:33182 => localhost/127.0.0.1:4454] ACTIVE
^Cthufir#dur:~/NetBeansProjects/NettyClient$
thufir#dur:~/NetBeansProjects/NettyClient$
server code:
package net.bounceme.dur.netty;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.net.Socket;
import java.util.logging.Logger;
#Sharable
public class PingPongServerHandler extends SimpleChannelInboundHandler<PingPong> {
private static final Logger log = Logger.getLogger(PingPongServerHandler.class.getName());
protected Socket socket = null;
#Override
protected void channelRead0(ChannelHandlerContext chc, PingPong pingPong) throws Exception {
log.info(pingPong.getClass().getSimpleName());
chc.writeAndFlush(new PingPong());
}
}
client code:
package net.bounceme.dur.netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.logging.Logger;
public class PingPongClientHandler extends SimpleChannelInboundHandler<PingPong> {
private static final Logger log = Logger.getLogger(PingPongClientHandler.class.getName());
public PingPongClientHandler() {
}
#Override
protected void channelRead0(ChannelHandlerContext chc, PingPong pingPong) throws Exception {
log.info("reading...");
chc.writeAndFlush(new PingPong());
}
}
adapted from:
https://github.com/netty/netty/tree/4.0/example/src/main/java/io/netty/example/objectecho
branch 4.0
see also:
https://community.jboss.org/wiki/NettyExampleOfPingPongUsingObject
which uses 3.x and not 4.x, and is overly complex for my purposes.
server:
src/
├── net
│ └── bounceme
│ └── dur
│ └── netty
│ ├── EchoServer.java
│ ├── MyProps.java
│ ├── PingPong.java
│ └── PingPongServerHandler.java
└── server.properties
client:
src
├── net
│ └── bounceme
│ └── dur
│ └── netty
│ ├── EchoClient.java
│ ├── MyProps.java
│ ├── PingPongClientHandler.java
│ └── PingPong.java
└── server.properties
I'm trying to adapt the Echo Netty example to use POJO's instead of ByteBuf.
Related
I am working on a maven multi-module project with the following structure
project/
├── migration/
│ ├── src/
│ │ ├── main/
│ │ │ └── Migration.java
│ │ └── test
│ └── pom.xml
├── depends-on-migration/
│ ├── src/
│ │ ├── main (uses Migration.java - ok)
│ │ └── test (uses Migration.java - throws NoClassDefFoundError)
│ └── pom.xml (depends on migration)
└── pom.xml
The class can be used (see image), but cannot compile when I run mvnw package (see logs)
2022-12-17T14:22:56.025+08:00 INFO 13468 --- [ main] c.b.d.DependsOnMigrationApplicationTests : Starting DependsOnMigrationApplicationTests using Java 17.0.2 with PID 13468 (started by Joseph in Z:\bwgjoseph\maven-nested-multi-module-project\depends-on-migration)
2022-12-17T14:22:56.031+08:00 INFO 13468 --- [ main] c.b.d.DependsOnMigrationApplicationTests : No active profile set, falling back to 1 default profile: "default"
2022-12-17T14:22:57.289+08:00 INFO 13468 --- [ main] c.b.d.DependsOnMigrationApplicationTests : Started DependsOnMigrationApplicationTests in 1.534 seconds (process running for 2.498)
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.493 s <<< FAILURE! - in com.bwgjoseph.dependsonmigration.DependsOnMigrationApplicationTests
[ERROR] test1 Time elapsed: 0.454 s <<< ERROR!
java.lang.NoClassDefFoundError: com/bwgjoseph/migration/Migration
at com.bwgjoseph.dependsonmigration.DependsOnMigrationApplicationTests.test1(DependsOnMigrationApplicationTests.java:17)
Caused by: java.lang.ClassNotFoundException: com.bwgjoseph.migration.Migration
at com.bwgjoseph.dependsonmigration.DependsOnMigrationApplicationTests.test1(DependsOnMigrationApplicationTests.java:17)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] DependsOnMigrationApplicationTests.test1:17 NoClassDefFound com/bwgjoseph/migr...
[INFO]
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0
This is a simplified example, can refer to repo (migration and depends-on-migration module)
I couldn't find anything similar, the closest I could find are cases where they are looking to use classes that are defined in migration/src/test but I am using migration/src/main
Glad to provide more information if required.
Thanks!
The problem stems from the fact that you use spring-boot-maven-plugin in both migration and depends-on-migration modules.
Spring Boot Maven Plugin produces an Uber-Jar, with a layout expected by Spring apps. The original jar (without dependencies) is renamed to migration-0.0.1-SNAPSHOT.jar.original
The structure of uber-jar
.
├── BOOT-INF
│ ├── classes
│ ├── classpath.idx
│ ├── layers.idx
│ └── lib
├── META-INF
├── migration-0.0.1-SNAPSHOT.zip
└── org
└── springframework
Your code is placed in BOOT-INF/classes - this is not a typical library layout, hence ClassNotFoundException.
See The Executable Jar Format for reference.
If you remove spring-boot-maven-plugin in migration project, mvn package passes.
This question already has an answer here:
Deploying Spring 5.x on Tomcat 10.x
(1 answer)
Closed 1 year ago.
Summary
I'm trying to create a basic Spring Boot application from scratch, build a .war file and run it in Tomcat. I achieved to run it in Intellij, but it is not running in Tomcat.
Reproduce
I explain in the following, how to reproduce my issue. Please write a comment if I'm missing anything relevant.
How to create the project?
Generate the project on https://start.spring.io/ using the following options
Click the button Generate. A file demo.zip is downloaded. Extract it as directory demo. It should contain the following files:
├── HELP.md
├── demo.iml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ └── ServletInitializer.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/example/demo/DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
src/main/java/com/example/demo/ServletInitializer.java
package com.example.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}
src/main/resources/application.properties is empty
Add a file src/main/java/com/example/demo/HelloWorldController.java with content
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloWorldController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
How to run the project in Intellij?
Open and load the maven project in Intellij. In the menu select Run and Run 'DemoApplication'. You should see something like this in the console:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)
2021-07-09 17:31:07.447 INFO 7245 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_292 on MY_HOST with PID 7245 (/path/to/demo/target/classes started by USER in /path/to/demo)
2021-07-09 17:31:07.451 INFO 7245 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-07-09 17:31:08.636 INFO 7245 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-07-09 17:31:08.647 INFO 7245 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-07-09 17:31:08.648 INFO 7245 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-07-09 17:31:08.720 INFO 7245 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-07-09 17:31:08.720 INFO 7245 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1205 ms
2021-07-09 17:31:09.214 INFO 7245 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-09 17:31:09.229 INFO 7245 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.89 seconds (JVM running for 3.783)
Open http://localhost:8080/ in your browser. You should see
Greetings from Spring Boot!
How to build the .war in Intellij?
Select View > Tool Windows > Maven in the menu and run lifecycles clean and package
Among other files, a file target/demo-0.0.1-SNAPSHOT.war is created.
How to run the project in Tomcat?
Note: I'm using Docker in the following to make the environment as reproducible as possible. However, I also tried Tomcat installed on another Mac using brew and got the same issue.
Run in the terminal (in the project root directory)
docker run --rm -p 8081:8080 \
-v ${PWD}/target/demo-0.0.1-SNAPSHOT.war:/usr/local/tomcat/webapps/demo.war \
tomcat:10-jdk8
You should get output similar to this:
09-Jul-2021 16:44:19.312 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/10.0.8
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 25 2021 23:05:41 UTC
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.8.0
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.10.25-linuxkit
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_292-b10
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
09-Jul-2021 16:44:19.335 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.30] using APR version [1.6.5].
09-Jul-2021 16:44:19.335 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
09-Jul-2021 16:44:19.339 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d 10 Sep 2019]
09-Jul-2021 16:44:19.821 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
09-Jul-2021 16:44:19.844 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [769] milliseconds
09-Jul-2021 16:44:19.898 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
09-Jul-2021 16:44:19.898 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.8]
09-Jul-2021 16:44:19.945 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/usr/local/tomcat/webapps/demo.war]
09-Jul-2021 16:44:21.248 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
09-Jul-2021 16:44:21.315 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/tomcat/webapps/demo.war] has finished in [1,369] ms
09-Jul-2021 16:44:21.321 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
09-Jul-2021 16:44:21.339 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1494] milliseconds
Note: I expect that the log of my app, i.e.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)
...
appears here too, but it is missing.
Open http://localhost:8081/demo/ in your browser. I expect to see
Greetings from Spring Boot!
but I get
What am I missing in my project or Tomcat configuration/code? How to run my Spring Boot application as .war in Tomcat?
As pointed out in the comments, running Tomcat 9 instead of 10 works
docker run --rm -p 8081:8080 \
-v ${PWD}/target/demo-0.0.1-SNAPSHOT.war:/usr/local/tomcat/webapps/demo.war \
tomcat:9-jdk8
I am trying to run a demo of spring web socket but not able to test it completely. I am using java 7 and tomcat 7.0.50. I don't get any error while server startup, but when I open the web page with js making the connection to it I got 404 page not found. I am not sure if I am missing anything in configuration to make it run and how can I be able to connect it from js side.
I have following xml file:
<beans ....>
<context:annotation-config />
<websocket:message-broker
application-destination-prefix="/app">
<websocket:stomp-endpoint path="/hello">
<websocket:sockjs />
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic" />
</websocket:message-broker>
</beans>
My controller class is:
#Controller
public class SwsService {
#MessageMapping("/hello")
#SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
return new Greeting("Hello, " + message.getName() + "!");
}
public String getGreeting() {
return "Hello, you are in!";
}
}
The js from which I am calling this is:
var sock = new SockJS("/hello");
sock.onopen = function () {
console.log("open");
};
sock.onclose = function () {
console.log("closed");
};
sock.onmessage = function (message) {
console.log("msg", message);
};
The console output when I run the tomcat:
Feb 19, 2014 3:28:47 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files\TortoiseGit\bin;C:\Python24;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Users\harsh\AppData\Roaming\npm;E:\IDE\eclipse_indigo;;.
Feb 19, 2014 3:28:47 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SWS' did not find a matching property.
Feb 19, 2014 3:28:48 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 19, 2014 3:28:48 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Feb 19, 2014 3:28:48 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1412 ms
Feb 19, 2014 3:28:48 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Feb 19, 2014 3:28:48 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.50
Feb 19, 2014 3:28:51 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Feb 19, 2014 3:28:51 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Feb 19, 2014 3:28:51 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Feb 19, 2014 3:28:51 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Wed Feb 19 15:28:51 IST 2014]; root of context hierarchy
Feb 19, 2014 3:28:52 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/conf/SwsContext.xml]
Feb 19, 2014 3:28:53 PM org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize
INFO: Initializing ExecutorService 'clientInboundChannelExecutor'
Feb 19, 2014 3:28:53 PM org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize
INFO: Initializing ExecutorService 'clientOutboundChannelExecutor'
Feb 19, 2014 3:28:53 PM org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize
INFO: Initializing ExecutorService 'messageBrokerSockJsScheduler'
Feb 19, 2014 3:28:53 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/hello/**] onto handler of type [class org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler]
Feb 19, 2014 3:28:53 PM org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup start
INFO: Starting beans in phase 2147483647
Feb 19, 2014 3:28:53 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 2173 ms
Feb 19, 2014 3:28:53 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Feb 19, 2014 3:28:53 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Feb 19, 2014 3:28:53 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5842 ms
The browser console output:
GET http://localhost:8080/hello/info 404 (Not Found) sockjs-0.3.min.js:27
closed
How can I successfully test it?
Updates
I also tried running the portfolio example from here: https://github.com/rstoyanchev/spring-websocket-portfolio as suggested by #jhadesdev
But it also doesn't help. When I run mvn tomcat7:run, I see following output and open url on browser tells 404.
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethrea
ded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-websocket-portfolio 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) # spring-websocket-portfol
io >>>
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) # spring-web
socket-portfolio ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\libraries\spring-websocket-portfol
io\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) # spring-websoc
ket-portfolio ---
[INFO] No sources to compile
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) # spring-websocket-portfol
io <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) # spring-websocket-portfol
io ---
[INFO] Running war on http://localhost:8080/spring-websocket-portfolio
[INFO] Using existing Tomcat server configuration at E:\libraries\spring-websock
et-portfolio\target\tomcat
[INFO] create webapp with contextPath: /spring-websocket-portfolio
Feb 27, 2014 10:20:46 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 27, 2014 10:20:46 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Feb 27, 2014 10:20:46 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Feb 27, 2014 10:20:49 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Feb 27, 2014 10:20:49 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
It stuck there and doesn't go ahead.
I was facing the same problem. This thread gave me enough clues to solve the problem. Thanks a lot.
SOLUTION:
In case you have a Spring MVC Dispatcher Servlet configured in your web.xml and mapped to a url-pattern as shown below
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/webui/*</url-pattern>
</servlet-mapping>
Then you have to create the SockJS instance as shown below
var socket = new SockJS('/contextPath/webui/hello');
NOTE: Replace contextPath with your application context path.
This Chrome extension can help you test Websockets.
Also you can have a look at my simple-chat project that uses Spring Boot with Websockets.
Try adding an assets folder inside webapp, where all the stomp and sockjs.js files are made available. Have a look at this example of a running application, that can be run with mvn clean install jetty:run.
The problem seems to be that either the sockjs is not on the server or some path is wrong. Based on the example on the link, a stomp endpoint can be configured like this:
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/portfolio").withSockJS();
}
}
Then in javascript the end point can be called like this:
var socket = new SockJS('/spring-websocket-portfolio/portfolio')
var stompClient = Stomp.over(socket);
where /spring-websocket-portfolio is the root deployment path of your application.
Your application looks fine.
You should definitely prefix JavaScript requests with the root context of your application, as jhadesdev mentioned.
Does your application registers additional filters or customize messageconverters? Maybe some other element of configuration is interfering here.
The latest portfolio example should definitely work, here are some questions:
Could you tell us more about your client setup (browser, version)?
Does your browser support websocket (test it there)?
Could you also try in incognito mode (maybe a 3rd party browser extension is at fault here)?
Could you check that your browser is not using a HTTP proxy for localhost requests?
Could you check if this HTTP 404 is in Tomcat access log? (and see if you've got something interesting in those or in catalina.out?)
How is your DispatcherServlet mapped? Here might be what you're looking for
I solved this problem in thats way:
when you create the SockJS add localhost:8080/
like this
new SockJS('http://localhost:8080/spring-websocket-portfolio/portfolio')
For me the solution was to enable SockJS in by WebSocketConfig class:
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/search")
.setAllowedOrigins("*")
.setHandshakeHandler(new DefaultHandshakeHandler())
.withSockJS();
}
I am following this example of a Hello World Wicket application
https://www.ibm.com/developerworks/web/library/wa-aj-wicket/
In particular I placed HelloWorld.html in my source directory next to HelloWorld.java.
My file structure looks like this:
$ tree
.
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── wicket
│ │ │ ├── HelloWorld.html
│ │ │ ├── HelloWorld.java
│ │ │ └── HelloWorldApplication.java
│ │ ├── resources
│ │ └── webapp
│ │ └── WEB-INF
│ │ └── web.xml
│ └── test
│ └── java
└── wicketTest.iml
However when I compile this to a war file, and load in Jetty, i recieve this error, in the browser:
Unexpected RuntimeException
Last cause: Can not determine Markup. Component is not yet connected to a parent. [Page class = com.example.wicket.HelloWorld, id = 4, render count = 1]
Stacktrace
Root cause:
org.apache.wicket.markup.MarkupNotFoundException: Can not determine Markup. Component is not yet connected to a parent. [Page class = com.example.wicket.HelloWorld, id = 4, render count = 1]
at org.apache.wicket.Component.getMarkup(Component.java:737)
at org.apache.wicket.Component.internalRender(Component.java:2344)
at org.apache.wicket.Component.render(Component.java:2307)
at org.apache.wicket.Page.renderPage(Page.java:1010)
When I look in the war file I notice that the html file is missing:
$ tar tvf target/wicketTest-1.0-SNAPSHOT.war
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/
-rwxrwxrwx 0 0 0 128 Aug 22 14:50 META-INF/MANIFEST.MF
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/com/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/com/example/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/com/example/wicket/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/lib/
-rwxrwxrwx 0 0 0 608 Aug 22 14:50 WEB-INF/classes/com/example/wicket/HelloWorld.class
-rwxrwxrwx 0 0 0 551 Aug 22 14:50 WEB-INF/classes/com/example/wicket/HelloWorldApplication.class
-rwxrwxrwx 0 0 0 25962 Aug 21 16:07 WEB-INF/lib/slf4j-api-1.6.4.jar
-rwxrwxrwx 0 0 0 2126440 Aug 21 16:07 WEB-INF/lib/wicket-core-6.10.0.jar
-rwxrwxrwx 0 0 0 86671 Aug 21 16:07 WEB-INF/lib/wicket-request-6.10.0.jar
-rwxrwxrwx 0 0 0 415858 Aug 21 16:07 WEB-INF/lib/wicket-util-6.10.0.jar
-rwxrwxrwx 0 0 0 690 Aug 22 13:22 WEB-INF/web.xml
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/maven/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/maven/wicketTest/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/maven/wicketTest/wicketTest/
-rwxrwxrwx 0 0 0 675 Aug 22 08:52 META-INF/maven/wicketTest/wicketTest/pom.xml
-rwxrwxrwx 0 0 0 112 Aug 22 14:50 META-INF/maven/wicketTest/wicketTest/pom.properties
How do I specify in my POM file to include the html file?
My POM right now is minimal:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wicketTest</groupId>
<artifactId>wicketTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>6.10.0</version>
</dependency>
</dependencies>
</project>
The solution, if you want your HTML in the wicket best practice place (with your classes) is to add this to the build section of your pom.
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
</project>
You should put you HelloWorld.html file into src/main/webapp folder. This way it will be included in the war file
If using Maven, see David Williams' answer. If using Gradle, see this answer.
Include the following in your build.gradle file:
sourceSets {
main {
resources {
srcDirs += ['src/main/java']
includes = ["**"]
// or specifically: includes = ["**/*.html"]
}
}
}
This ensures that the HTML files will be added to the WAR file.
I am trying to set up a mysql database in a JBoss project. I am getting errors about JBAS014775: New missing/unsatisfied dependencies. I have read countless posts on SO and I am still not sure why these problems are arising. I have pasted the error logs from the console down below
The module file looks as follows:
<?xml version = "1.0" encoding = "UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.23-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
The persistence.xml looks as follows:
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/greeter-quickstart-ds.xml,
you can find it in the source at src/main/webapp/WEB-INF/greeter-quickstart-ds.xml -->
<jta-data-source>java:jboss/datasources/GreeterQuickstartDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
The greeter-quickstart-ds.xml appears as follows
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<!-- The datasource is bound into JNDI at this location. We reference
this in META-INF/persistence.xml -->
<datasource jndi-name="java:jboss/datasources/GreeterQuickstartDS"
pool-name="greeter-quickstart" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/jboss</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
</datasource>
</datasources>
The standalone.xml appears as follows:
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:3306/jboss</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
<statement>
<prepared-statement-cache-size>100</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MySqlXADataDource</xa-datasource-class>
</driver>
</drivers>
</datasources>
02:04:44,660 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
02:04:44,706 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
02:04:45,369 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) JBAS010400: Bound data source [java:jboss/datasources/MySqlDS]
02:04:45,371 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-8) JBAS015012: Started FileSystemDeploymentService for directory /opt/jboss-as-7.1.0.Final/standalone/deployments
02:04:45,373 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-5) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
02:04:45,377 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015014: Re-attempting failed deployment greeter.war
02:04:45,476 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.0.Final "Thunder" started in 2540ms - Started 138 of 210 services (70 services are passive or on-demand)
02:04:45,491 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "greeter.war"
02:04:45,816 INFO [org.jboss.as.jpa] (MSC service thread 1-7) JBAS011401: Read persistence.xml for primary
02:04:45,900 INFO [org.jboss.weld.deployer] (MSC service thread 1-4) JBAS016002: Processing weld deployment greeter.war
02:04:46,332 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS015870: Deploy of deployment "greeter.war" was rolled back with failure message {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.data-source.jboss/datasources/GreeterQuickstartDSjboss.jdbc-driver.mysql-connector-java-5_1_23-bin_jarMissing[jboss.data-source.jboss/datasources/GreeterQuickstartDSjboss.jdbc-driver.mysql-connector-java-5_1_23-bin_jar]"]}
02:04:46,348 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-5) JBAS010409: Unbound data source [jboss/datasources/GreeterQuickstartDS]
02:04:46,354 INFO [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015877: Stopped deployment greeter.war in 22ms
02:04:46,355 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014775: New missing/unsatisfied dependencies:
service jboss.jdbc-driver.mysql-connector-java-5_1_23-bin_jar (missing) dependents: [service jboss.data-source.jboss/datasources/GreeterQuickstartDS]
├── google
│ └── guava
│ └── main
│ ├── guava-10.0.1.jar
│ ├── guava-10.0.1.jar.index
│ └── module.xml
├── h2database
│ └── h2
│ └── main
│ ├── h2-1.3.161.jar
│ ├── h2-1.3.161.jar.index
│ └── module.xml
├── mysql
│ └── main
│ ├── module.xml
│ ├── mysql-connector-java-5.1.23-bin.jar
│ └── mysql-connector-java-5.1.23-bin.jar.index
└── sun
├── jsf-impl
│ ├── 1.2
│ │ ├── jsf-impl-1.2_15-jbossorg-2.jar
│ │ └── module.xml
│ └── main
│ ├── jsf-impl-2.1.5-jbossorg-1.jar
│ ├── jsf-impl-2.1.5-jbossorg-1.jar.index
│ └── module.xml
└── xml
├── bind
│ └── main
│ ├── jaxb-impl-2.2.4.jar
│ ├── jaxb-impl-2.2.4.jar.index
│ ├── jaxb-xjc-2.2.4.jar
│ ├── jaxb-xjc-2.2.4.jar.index
│ └── module.xml
└── messaging
└── saaj
└── main
├── module.xml
├── saaj-impl-1.3.16-jbossorg-1.jar
└── saaj-impl-1.3.16-jbossorg-1.jar.index
02:04:46,356 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.data-source.jboss/datasources/GreeterQuickstartDSjboss.jdbc-driver.mysql-connector-java-5_1_23-bin_jarMissing[jboss.data-source.jboss/datasources/GreeterQuickstartDSjboss.jdbc-driver.mysql-connector-java-5_1_23-bin_jar]"]}}}
You need to only provide the unique name for the driver <driver>com.mysql</driver> and not the entire jar name. In your case you have <driver>mysql</driver> in one definition and the entire jar name in another.
For more information please refer here on how to setup a datasource
I have been able to connect to a mysql db now. Originally, I had added a datasource by manually tweaking the standalone.xml file. I would advise you not to do this. Rather, you should use the admin web console provided by JBoss to add a datasource instead