1

I have a project that works perfectly if I deploy it outside of a JAR. However, it is unable to get sessions when its deployed as a JAR. Here are the artifacts:

Main.java:

public class Main {
    public static void main(String[] args) throws Exception{
        JndiContext registry=new JndiContext();

        CamelContext context=new DefaultCamelContext(registry);
        context.addRoutes(new RouteBuilder() {      
            @Override
            public void configure() {
                try{
                    from("jetty:http://0.0.0.0:7700?matchOnUriPrefix=true&sessionSupport=true").bean(HtmlProcessor.class);
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        });

        context.start();
        Thread.sleep(1000000);
        context.stop();

    }
}

HtmlProcessor.java:

public class HtmlProcessor {
    public String login(Exchange exchange){
        HttpSession session=exchange.getIn(HttpServletRequest.class).getSession();
        Integer count=(Integer)session.getAttribute("count");
        if (count==null) count=0;
        count++;
        session.setAttribute("count", count);

        StringBuilder sb=new StringBuilder();
        sb.append("<html><body><form>");
        sb.append(count);
        sb.append("<BR><input type=text name=username></form></body></html>");
        return sb.toString();
    }
}

pom.xml

<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>user1</groupId>
  <artifactId>jettysession</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
        <camel.version>2.10.3</camel.version>
  </properties>
  <dependencies>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
            <version>${camel.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.13</version>
        </dependency>

  </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>assemble</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Bundle-ClassPath>.</Bundle-ClassPath>
                            <Main-Class>user1.Main</Main-Class>
                        </manifestEntries>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Here is the meaningful part of the exception:

java.lang.NullPointerException
    at user1.HtmlProcessor.login(HtmlProcessor.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:341)
...
User1
  • 39,458
  • 69
  • 187
  • 265
  • And what is code line 10 of HtmlProcessor.java do? – Claus Ibsen Jan 30 '13 at 07:11
  • This is line 10 "HttpSession session=exchange.getIn(HttpServletRequest.class).getSession();". Basically, the getIn() return null. I'm so glad to see your name! I know you are deeply involved in camel and can help. – User1 Jan 30 '13 at 14:44
  • Can you explain in more detail to other users as well, what that maven assembly does for your project. Does it put all the JARs into one uber JAR, and how do you run that JAR etc. If so take extra care about that some files may override other files, causing the uber JAR to not be fully complete. – Claus Ibsen Jan 30 '13 at 15:46
  • http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven explains the jar-with-dependencies. Yes..it is an uber JAR. Any suggestions on where to look for overriding files? – User1 Jan 30 '13 at 15:56
  • Also, to run the jar just type `java -jar jettysession-0.0.1-SNAPSHOT-jar-with-dependencies.jar`. Then, open a browser to `http://localhost:7700/?username=a`. Each time you load the page, you should see a number increase by 1. – User1 Jan 30 '13 at 16:47

1 Answers1

1

There is a file in META-INF/services/org/apache/camel/ named TypeConverter, which you need to merge with data from all the JARs. When you do this uber JAR I am pretty sure the TypeConverter file is possible just the last file, and therefore you lose data.

See this FAQ at Camel: http://camel.apache.org/how-do-i-use-a-big-uber-jar.html

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Thank you. In my uber JAR, that file has a bunch a comments and the line `org.apache.camel.core`. Should it say something more? – User1 Jan 30 '13 at 17:43
  • That seems to have fixed it. I used the pom.xml segment at https://issues.apache.org/jira/browse/CAMEL-3773?focusedCommentId=13005004&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13005004. It basically uses a shaded JAR but also excludes a bunch of resources. – User1 Jan 30 '13 at 18:58
  • For the curious, I was missing the following lines on the TypeConverter file: `org.apache.camel.component.jetty.JettyConverter`, `org.apache.camel.component.http.HttpConverter`,`org.apache.camel.component.http.RequestEntityConverter` – User1 Jan 30 '13 at 19:01