user6461129
user6461129

Reputation:

Deploying a project on external Tomcat

I've followed the following Spring Web Sockets guide to create an application using Spring Web Sockets on stomp. https://spring.io/guides/gs/messaging-stomp-websocket/ The application works fine when I run it using the Spring Boot embedded Tomcat. however now I want to deploy it on a local instance of Tomcat 7.

I've modified the Application class to the following,

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(applicationClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.scan(WebSocketConfig.class.getPackage().getName());

        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(context));

        appServlet.setLoadOnStartup(1);
        Set<String> mappings = appServlet.addMapping("/app");

        if (!mappings.isEmpty()) {
            throw new IllegalStateException("Conflicting mappings found! Terminating. " + mappings);
        }
    }

    private static Class<Application> applicationClass = Application.class;
}

The WebSocket Configuration is as follows,

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}

And my build.gradle is as follows,

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

war {
    baseName = 'opl-ws-webui'
    version =  '0.1.0'
}
repositories {
    jcenter()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

bootRepackage {
    enabled false
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-websocket")
    compile("org.springframework:spring-messaging")
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.1'
    compile files('lib/opla230.jar')
    //providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

When I right click the project and run it on server from inside eclipse, it serves the correct index.html at the following url. http://localhost:8080/opl-ws-webui/

However when I press the connect button which is supposed to open up a text input box I get nothing. On checking the developer tools, here's the error message that I get.

sockjs-0.3.4.js:807 GET http://localhost:8080/hello/info 404 (Not Found)
Whoops! Lost connection to undefined

I've googled the error message and tried implementing the suggestions in the following links, Whoops! Lost connection to undefined - Connection Lost Just After The Connection Established Stomp over socket using sockjs can't connect with Spring 4 WebSocket

And yet I continue to get the same error. I've tried deploying on both Tomcat 7 and 8 and still the same. Really appreciate some help on the issue.

Upvotes: 8

Views: 1570

Answers (5)

AdamIJK
AdamIJK

Reputation: 655

You can not run the app with IDE itself, you have to make a war file and deploy it in tomcat it will be run. make sure that the port number 8080 not used by any other servers/apps.

Upvotes: 0

Akash Mishra
Akash Mishra

Reputation: 712

SockJS doesn't honour relative URLs. Find an approach to a related problem here

Upvotes: 0

Md Samiul Alim Sakib
Md Samiul Alim Sakib

Reputation: 1114

Maybe problem with application context. When you are running the app in your local system it map to context root ( / ). But when you upload it to tomcat the context will be your file name. Ex, if your file name is "myapp" the context will be http://server:8080/myapp though in local system it runs as root app. You can solve this problem by setting your app as root context of tomcat or change the app paths to production context ( in this case myapp). To set ROOT app go to (Ubuntu) :

/var/lib/tomcat7/conf/server.xml

Then add,

<Context path="ROOT" docBase="myapp">
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

and remove other root config if any. Finally restart tomcat server.

Upvotes: 1

Skyware
Skyware

Reputation: 352

I use Apache Tomcat Maven Plugin for this purpose but you are using Gradle. It works perfectly with Tomcat 7 but does not support Tomcat 8. Please, see this [link][1]. http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/

Upvotes: 0

akgaur
akgaur

Reputation: 785

If you are using Spring Boot, then you could switch to war packaging instead of jar and change to provided scope of tomcat dependency.

For more details check :

Jar to war

Build config

Serve static content

Upvotes: 1

Related Questions