2012-02-10 4 views
0

Один из моих партнеров нуждается в SOAP-интерфейсе, поэтому я подумал: перейдите на сервер приложений. Я выбрал Glassfish, и он отлично работает из коробки. Есть только одна вещь, которую я не могу сделать: введи @SessionScoped ManagedBean в аннотированный EJB @Webservice.Доступ к @SessionScoped bean из @WebService аннотированный EJB

Единственный способ, которым я могу получить EJB, - это над JNDI. Я прочитал об этом here

Мой вопрос: есть ли ошибка в Glassfish, и он будет работать в будущей версии или должен работать, и я сделал что-то не так.

Я хочу, чтобы иметь возможность ввести свойство DataAccess session с аннотацией @EJB, но это не сработает.

Ниже приведены все файлы в тестовом проекте

Вот мой WebService класс:

@LocalBean 
@Stateless 
@WebService() 
public class MySOAP implements Serializable { 

    private DataAccess session; 

    @WebMethod 
    public String getUsername() { 
     javax.naming.Context ctx = null; 
     try { 
     ctx = new javax.naming.InitialContext(); 
     session = (DataAccess) ctx.lookup("java:comp/env/DataAccess"); 
     return "user is " + session.getData(); 
     } catch (NamingException e) { 
     e.printStackTrace(); 
     } 
     return "exception occured"; 
    } 
} 

EJB-я хотел бы внедрить:

public abstract class AbstractDataBean { 
    @Inject /* this just returns some text*/ 
    private MySessionBean session; 

    public MySessionBean getSession() { 
     return session; 
    } 
} 

@LocalBean 
@Stateless 
public class DataAccess extends AbstractDataBean implements Serializable { 

    public String getData() { 
     return " data " + getSession(); 
    } 

    @Override 
    public String toString() { 
     return getData(); 
    } 
} 

@Named 
@SessionScoped 
public class MySessionBean implements Serializable { 
    static private int classCounter = 0; 
    private String user; 

    @PostConstruct 
    public void initMySessionBean() { 
     user = "Micha " + (++classCounter) ; //to check how many times it was called 
    } 

    public String getUser() { 
     return user; 
    } 

    public void setUser(String user) { 
     this.user = user; 
    } 

    @Override 
    public String toString() { 
     return user; 
    } 
} 

У меня есть сеть. xml для определения JNDI:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     version="3.0"> 

    <session-config> 
     <session-timeout>1</session-timeout> 
    </session-config> 

    <ejb-local-ref> 
     <ejb-ref-name>DataAccess</ejb-ref-name> 
     <ejb-ref-type>Session</ejb-ref-type> 
     <local>ch.sertal.server.services.ejb.DataAccess</local> 
    </ejb-local-ref> 

</web-app> 

солнцем jaxws.xml определить SOAP WebService:

<?xml version="1.0" encoding="UTF-8"?> 
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'> 

    <endpoint 
       name='Mysoap' 
       implementation='ch.sertal.server.services.MySOAP' 
       url-pattern='/soap/Mysoap '/> 
</endpoints> 

и пустой beans.xml, чтобы иметь работу CDI:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> 
</beans> 

и, наконец, 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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>ch.sertal</groupId> 
    <artifactId>VisionWeb</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>VisionWeb Jersey Webapp</name> 
    <build> 
     <finalName>VisionWeb</finalName> 
     <resources> 
     <resource> 
      <filtering>true</filtering> 
      <directory>src/main/resources</directory> 
      <targetPath>META-INF</targetPath> 
      <includes> 
       <include>*.xml</include> 
      </includes> 
     </resource> 

     <resource> 
      <filtering>true</filtering> 
      <directory>src/main/resources</directory> 
      <targetPath>.</targetPath> 
      <includes> 
       <include>*.properties</include> 
      </includes> 
     </resource> 

     <resource> 
      <filtering>true</filtering> 
      <directory>src/main/resources/i18n</directory> 
      <targetPath>.</targetPath> 
      <includes> 
       <include>*.properties</include> 
      </includes> 
     </resource> 
     </resources> 
     <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <inherited>true</inherited> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.glassfish.maven.plugin</groupId> 
      <artifactId>maven-glassfish-plugin</artifactId> 
      <version>2.1</version> 
      <configuration> 
       <glassfishDirectory>/Development/glassfish3</glassfishDirectory> 
       <user>admin</user> 
       <adminPassword>U36c9AqVf5Ppk4DX</adminPassword> 
       <autoCreate>true</autoCreate> 
       <debug>true</debug> 
       <echo>false</echo> 
       <terse>true</terse> 
       <skip>false</skip> 
       <domain> 
        <host>${glassfish.host}</host>> 
        <name>domain1</name> 
        <adminPort>4848</adminPort> 
        <httpPort>9080</httpPort> 
        <httpsPort>8181</httpsPort> 
        <iiopPort>3700</iiopPort> 
        <jmsPort>7676</jmsPort> 
       </domain> 
       <components> 
        <component> 
        <name>VisionWeb</name> 
        <artifact>${project.build.directory}/${project.build.finalName}.war</artifact> 
        </component> 
       </components> 
      </configuration> 
     </plugin> 

     <!--surefire--> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.9</version> 
      <configuration> 
       <parallel>test</parallel> 
       <threadCount>1</threadCount> 
       <groups>${test.groups}</groups> 
      </configuration> 
     </plugin> 

     </plugins> 
    </build> 
    <dependencies> 

     <dependency> 
     <groupId>org.jboss.weld</groupId> 
     <artifactId>weld-api</artifactId> 
     <version>1.1.Final</version> 
     </dependency> 

     <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
     </dependency> 

     <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <version>6.0</version> 
     </dependency> 

     <!--jersey for RESTful services--> 
     <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-server</artifactId> 
     <version>${jersey-version}</version> 
     </dependency> 
     <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-json</artifactId> 
     <version>${jersey-version}</version> 
     </dependency> 
     <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-client</artifactId> 
     <version>${jersey-version}</version> 
     <scope>test</scope> 
     </dependency> 
     <dependency> 
     <groupId>com.sun.jersey.contribs</groupId> 
     <artifactId>jersey-multipart</artifactId> 
     <version>${jersey-version}</version> 
     </dependency> 

     <!--GlassFish libraries--> 
     <dependency> 
     <groupId>org.glassfish.distributions</groupId> 
     <artifactId>web-all</artifactId> 
     <version>10.0-build-20080430</version> 
     <scope>test</scope> 
     </dependency> 
     <dependency> 
     <groupId>org.glassfish.embedded</groupId> 
     <artifactId>gf-embedded-api</artifactId> 
     <version>1.0-alpha-4</version> 
     <scope>test</scope> 
     </dependency> 

     <!--peristence & database--> 

     <!-- hsqldb --> 
     <dependency> 
     <groupId>org.hsqldb</groupId> 
     <artifactId>hsqldb</artifactId> 
     <version>2.2.6</version> 
     </dependency> 

     <!-- persistence --> 
     <dependency> 
     <groupId>org.eclipse.persistence</groupId> 
     <artifactId>eclipselink</artifactId> 
     <version>${eclipselink.version}</version> 
     <scope>compile</scope> 
     </dependency> 
     <dependency> 
     <groupId>org.eclipse.persistence</groupId> 
     <artifactId>javax.persistence</artifactId> 
     <version>${eclipselink.jpa.version}</version> 
     <scope>compile</scope> 
     </dependency> 

     <!--SHIRO Authentication--> 
     <dependency> 
     <groupId>org.apache.shiro</groupId> 
     <artifactId>shiro-core</artifactId> 
     <version>1.1.0</version> 
     </dependency> 

     <dependency> 
     <groupId>org.apache.shiro</groupId> 
     <artifactId>shiro-web</artifactId> 
     <version>1.1.0</version> 
     </dependency> 

     <!--POI components--> 
     <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi</artifactId> 
     <version>3.7</version> 
     </dependency> 

     <!--PDF Box--> 
     <dependency> 
     <groupId>org.apache.pdfbox</groupId> 
     <artifactId>pdfbox</artifactId> 
     <version>1.6.0</version> 
     </dependency> 

     <!--XML processing--> 
     <dependency> 
     <groupId>dom4j</groupId> 
     <artifactId>dom4j</artifactId> 
     <version>1.6.1</version> 
     </dependency> 

     <!--testing--> 
     <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.2</version> 
     <scope>test</scope> 
     </dependency> 
     <dependency> 
     <groupId>org.testng</groupId> 
     <artifactId>testng</artifactId> 
     <version>6.0.1</version> 
     <scope>test</scope> 
     </dependency> 

    </dependencies> 

    <!--properties--> 
    <properties> 
     <jersey-version>1.8</jersey-version> 
     <jpa.unit>hsqldb.server</jpa.unit> 
     <test.jpa.unit>hsqldb.testr</test.jpa.unit> 

     <eclipselink.version>2.3.0</eclipselink.version> 
     <eclipselink.jpa.version>2.0.3</eclipselink.jpa.version> 
    </properties> 

    <!--profiles--> 
    <profiles> 
     <profile> 
     <id>mode.alpha.dev</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
      <property> 
       <name>environment</name> 
       <value>server</value> 
      </property> 
     </activation> 
     <properties> 
      <delete.files>true</delete.files> 

      <log.level>FINER</log.level> 
      <jersey.log.level>WARNING</jersey.log.level> 
      <jpa.log.level>INFO</jpa.log.level> 
      <test.groups>server-tests</test.groups> 
      <db.server.name>localhost</db.server.name> 
      <img-basedir>/opt/sertal/data</img-basedir> 

      <!--the url for tomcat 7 has changed. this is why /html has been appended to the below URL--> 
      <tomcat.manager>http://dev.sertal.net:7070/manager/html</tomcat.manager> 
      <tomcat.username>sertaladmin</tomcat.username> 
      <tomcat.password>29VeK0Ul</tomcat.password> 
      <tomcat.context>/VisionWeb</tomcat.context> 

      <test.groups>none</test.groups> 

      <!-- the date at the end marks the version of the data model --> 
      <hsqldb.data.path>/opt/sertal/data/hsqldb-dev-20110813</hsqldb.data.path> 
     </properties> 
     </profile> 
    </profiles> 

    <!--repositories and plugin repos--> 
    <repositories> 
     <!--glass fish--> 
     <repository> 
     <id>glassfish.java.net</id> 
     <name>GlassFish Maven Repository</name> 
     <url>http://download.java.net/maven/glassfish</url> 
     <layout>default</layout> 
     </repository> 
     <repository> 
     <id>m2.java.net</id> 
     <name>Java.net Maven 2 Repository</name> 
     <url>http://download.java.net/maven/2</url> 
     <layout>default</layout> 
     </repository> 
     <!--eclipse link--> 
     <repository> 
     <id>eclipselink.repo</id> 
     <name>eclipselink maven repository</name> 
     <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url> 
     <snapshots> 
      <enabled>true</enabled> 
     </snapshots> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
     <id>m2.java.net</id> 
     <name>Java.net Maven 2 Repository</name> 
     <url>http://download.java.net/maven/2</url> 
     <layout>default</layout> 
     </pluginRepository> 

     <pluginRepository> 
     <id>jboss-public-repository-group</id> 
     <name>JBoss Public Maven Repository Group</name> 
     <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> 
     <layout>default</layout> 
     <releases> 
      <enabled>true</enabled> 
      <updatePolicy>never</updatePolicy> 
     </releases> 
     <snapshots> 
      <enabled>true</enabled> 
      <updatePolicy>never</updatePolicy> 
     </snapshots> 
     </pluginRepository> 
    </pluginRepositories> 
</project> 

Запасной себе совет, я не переключится на JBoss :-)

ответ

5

Сессия Область не активен веб черпает в разделе 6.7.2 из JSR 299, это не ошибка. Если вы думаете об этом, нет смысла, поскольку сеанс действительно не следует из одного запроса веб-службы к другому.

Кроме того, вы сказали, что не будете переключены на JBoss, из любопытства, каковы ваши причины?

+0

Благодарим за информацию. Тогда я буду придерживаться JNDI. Это не большое дело. –

+1

Что касается JBoss: , когда я установил Glassfish, он просто сработал. Я мог бы без проблем развернуть мой существующий webapp из Tomcat. На JBoss этого не произошло. Возможно, это связано с отсутствием знаний на моей стороне и тем фактом, что я использую eclipse-link для своих ORM и Jersey для своих веб-сервисов RESTful, которые не являются библиотеками JBoss. Во всяком случае, Glassfish сделал лучшее первое впечатление и с серверами приложений, как с людьми, первые впечатления посчитали много :-) –

+0

Должен сказать, Glassfish также впечатляет меня. – SoftwareSavant

Смежные вопросы