1

如果我将它部署在 JAR 之外,我有一个可以完美运行的项目。但是,当它部署为 JAR 时,它无法获取会话。以下是文物:

主.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>

这是异常的有意义的部分:

java.lang.NullPointerException
    在 user1.HtmlProcessor.login(HtmlProcessor.java:10)
    在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)
    在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)
    在 java.lang.reflect.Method.invoke(未知来源)
    在 org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:341)
...
4

1 回答 1

1

在 META-INF/services/org/apache/camel/ 中有一个名为 TypeConverter 的文件,您需要将其与所有 JAR 中的数据合并。当您执行此 uber JAR 时,我很确定 TypeConverter 文件可能只是最后一个文件,因此您会丢失数据。

请参阅 Camel 的此常见问题解答:http: //camel.apache.org/how-do-i-use-a-big-uber-jar.html

于 2013-01-30T17:28:24.107 回答