2013-07-15 17 views
5

SOAPサービスとRESTサービスがアプリケーションサーバの外でJersey(Grizzly)を使用して同じポートを共有できるようにする方法を知っている人はいますか?同じポートでSOAPサービスとRESTサービスをホストする方法は?

  • 私のSOAPサービスがwww.someurl.com:port/soap/crm
  • で私の残りのサービスは、これらのサービスは、同じを共有www.someurl.com:port/crm

です同じベースURLではないので、そのポートで並行して実行できるはずです。しかし、ポートバインドエラーがあります。

これはすべてカスタムサービスアプリケーションにあり、web.xmlなどはありません。

RESTサービスはJerseyを使用しており、Soapサービスはエンドポイントに公開されたクラス 'ReceptionService'です。

URI soapUri = URI.create("192.168.0.0:1234\soap\Crm") 
URI restUri = URI.create("192.168.0.0:1234\crm") 

// START SOAP SERVICE 
Object reception = getObjectResource(ReceptionService.class); 
Endpoint e = Endpoint.publish(soapUri, reception); 

// START REST SERVICE  
ResourceConfig rc = new ResourceConfig().packages("company.rest"); 
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(restUri, rc); 

私はこれをしようとすると、グリズリーは 'java.net.BindExceptionを:使用中のすでに住所:バインド' 吐き出す:

@WebService(targetNamespace = "company.crm") 
    @SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL,  parameterStyle = SOAPBinding.ParameterStyle.WRAPPED) 
    public class Reception implements ReceptionService { 
    ... methods 
    } 

マイ

マイSOAPサービスの受信は、このような設定でありますレストサービスクラスには、いつものようにアノテートされています。

@Path("hello/{username}") 
public class Hello { ... } 

私はこれで少し初心者ですどんなポインタも評価されるでしょう。そして、私がアプリケーションサーバーを実行することを示唆しないでください。それはここでは問題ではありません - 問題は、他のWebサービスフレームワークに移動せずにポートバインドの問題を回避する方法です。

NOTE REのBOUNTY:私は恵みの勝者は、同じエンドポイント上で一緒にRESTのためにジャージーの使用方法を示すとJava SOAPサービス(注釈付きJAX-WS)だろう」THIS ENOUGH

を反復処理することはできません答えます。 RestおよびSoapアノテーション付きクラスを変更する必要はありませんが、HTTPサーバーコードの変更や設定変更が問題ない場合は、Jettyまたは他のアプリケーションサーバーに切り替えることはできません。ソリューションは100%埋め込みで実行する必要があります。 Java SEおよびJava Webサービスライブラリ」を参照してください。

+0

さらに詳しい情報が役立ちますどのタイプのWebサーバー/アプリケーションサーバーを使用しますか? 'web.xml'はどのように見えますか? –

+0

解決策を見つけましたか?私は同じ問題に直面している –

+0

いいえ。私はまだそれらを別々のポートに保っています。私は自分のコードを改訂しなければならないでしょうし、時間も忍耐もありません。将来的にはそれを設定することができます。 –

答えて

1

Webサービス用のtomcatなどの共通のコンテナを使用している場合は、両方のサービスが同じポートに到着する要求を受け取ることができます。アプリケーションの一部として、RESTおよびSOAPベースのサービスの両方をデプロイできます。コンテナは着信要求を受け入れ、アプリケーションコンテキストに応じてアプリケーションに転送します。アプリケーションweb.xmlでは、リクエストURLのマッピングに応じてリクエストを送信する場所を設定できます。

+0

ありがとうございます。私はJersey(Glassfishの外部)を顧客サーバーアプリケーションで使用しています。私は上記のより多くの情報を掲示し、誰かが解決策を持っているかもしれない。 –

2

あなたが言っているのは、サービスを公開するための2つの異なるインターフェイスだけです。はい、単一のポートでホストすることができます。同じコンテナに展開するだけで、両方のインターフェイスを稼働させることができます。

コンテキストパスの競合がないことを確認してください。これは、上記の質問に記載されているURLでは発生していないようです。

残りのインターフェイスを次のように展開します。

www.someurl。com:port2/crmなので、soapは同じURL(www.someurl.com:port1/soap/crm)に配置しないでください。これは問題ありません。

また、インターフェイスを展開する方法を別々のwarファイルまたは1つのwarファイルで説明する必要があります。

+0

ありがとうございます。しかし、同じURLポートを共有する必要があります。上記のようにコード化されたアプリケーションファイルはありません。 –

+0

ここで問題となるのは、Endpoint.publishが開始され、HTTP ServerとGrizzlyの部分もHTTPを開始します(つまり、エンドポイントが実際に異なっていても同じポートを共有するとバインド例外が発生します)。このため、ポートクラッシュが発生しています。 –

+0

それでは、どうやってグリズリーに石けんサービスを提供するのですか? IE。受信サービスを適切に並べて公開しますか? Microsoft ServerとApache CMXでは同じ動作が可能なので、方法が必要です。 –

1

同じポートで複数のサービスを開始することはできません。

アプリケーションが同じポートでアクセスできるようにするには、アプリケーション サーバーを使用する必要があります。

サーバー上のAppsには別のURL(web.xml)でアクセスできるように注意する必要があります。

同じアプリケーションサーバー上のすべてのアプリケーションが、同じポートでアクセスできるようになりました。

+0

アプリケーションサーバーで実行できる場合は、アプリケーションサーバーなしでプログラムで実行できます。どのようにしてGlassfishは、残りのサーバーと石鹸サービスとしてジャージー(グリズリーhttp)を使用して同じポート上でホストしています...それは百万ドルの質問です:) –

4

Jettyのオーバーレイ機能を使用すると、異なるJettyインスタンス/ポートに異なるパスを持つ2つの異なるWebアプリケーションを展開できます。

Jerseyサーブレット(REST用)に1つのweb.xmlがあり、SOAPサーブレットにもう1つのweb.xmlがあります。

http://www.eclipse.org/jetty/documentation/current/overlay-deployer.html

+0

OK。もし私がそれをJettyに移したら、それはうまくいくかもしれません。そして、私は今、物事の穏やかな側面のために桟橋を使用しているので、私はGuiceで簡単に設定する方法が好きなので、おそらく石鹸もそのように行くことができます。 –

+0

しかし、それはweb.xmlファイルを含まないソリューションでなければなりません。あなたがJettyとXMLファイルなしでこれを行う方法を示すことができるなら、私はそれを適切な答えと考えます。 –

0

これは、構成Iが完全に(明らかグリズリーを使用して)私のアプリに埋め込まれた単一のポート(2つの異なるコンテキストパス)の両方休息およびSOAPサービスをホストするために使用するコード、及びスプリングのほとんどです。 ..

package com.mycompany.structure.web.grizzly; 

import java.io.IOException; 
import java.lang.reflect.Constructor; 
import java.net.BindException; 
import java.util.EnumSet; 
import java.util.LinkedList; 
import javax.servlet.DispatcherType; 
import javax.servlet.Servlet; 
import com.mycompany.structure.web.jersey.jackson.JsonResourceConfig; 
import com.mycompany.structure.web.jersey.spring.ExposedApplicationContext; 
import org.glassfish.grizzly.http.server.HttpHandler; 
import org.glassfish.grizzly.http.server.HttpServer; 
import org.glassfish.grizzly.http.server.NetworkListener; 
import org.glassfish.grizzly.jaxws.JaxwsHandler; 
import org.glassfish.grizzly.servlet.WebappContext; 
import org.glassfish.jersey.servlet.ServletContainer; 
import org.smallmind.nutsnbolts.lang.web.PerApplicationContextFilter; 
import org.springframework.beans.BeansException; 
import org.springframework.beans.factory.DisposableBean; 
import org.springframework.beans.factory.config.BeanPostProcessor; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 
import org.springframework.context.ApplicationEvent; 
import org.springframework.context.ApplicationListener; 
import org.springframework.context.event.ContextRefreshedEvent; 

public class GrizzlyInitializingBean implements DisposableBean, ApplicationContextAware, ApplicationListener, BeanPostProcessor { 

    private static final Class[] NO_ARG_SIGNATURE = new Class[0]; 
    private HttpServer httpServer; 
    private LinkedList<WebService> serviceList = new LinkedList<>(); 
    private ServletInstaller[] servletInstallers; 
    private String host; 
    private String contextPath; 
    private String restPath; 
    private String soapPath; 
    private int port; 
    private boolean debug = false; 

    public void setHost (String host) { 

    this.host = host; 
    } 

    public void setPort (int port) { 

    this.port = port; 
    } 

    public void setContextPath (String contextPath) { 

    this.contextPath = contextPath; 
    } 

    public void setRestPath (String restPath) { 

    this.restPath = restPath; 
    } 

    public void setSoapPath (String soapPath) { 

    this.soapPath = soapPath; 
    } 

    public void setServletInstallers (ServletInstaller[] servletInstallers) { 

    this.servletInstallers = servletInstallers; 
    } 

    public void setDebug (boolean debug) { 

    this.debug = debug; 
    } 

    @Override 
    public synchronized void onApplicationEvent (ApplicationEvent event) { 

    if (event instanceof ContextRefreshedEvent) { 

     if (debug) { 
     System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true"); 
     } 
     httpServer = new HttpServer(); 
     httpServer.addListener(new NetworkListener("grizzly2", host, port)); 

     WebappContext webappContext = new WebappContext("Grizzly Application Context"); 
     webappContext.addServlet("JAX-RS Application", new ServletContainer(new JsonResourceConfig(ExposedApplicationContext.getApplicationContext()))).addMapping(restPath + "/*"); 
     webappContext.addFilter("per-application-data", new PerApplicationContextFilter()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), restPath + "/*"); 
     webappContext.addListener("org.springframework.web.context.request.RequestContextListener"); 

     for (ServletInstaller servletInstaller : servletInstallers) { 
     try { 

      Constructor<? extends Servlet> servletConstructor; 
      Servlet servlet; 
      String urlPattern; 

      servletConstructor = servletInstaller.getServletClass().getConstructor(NO_ARG_SIGNATURE); 
      servlet = servletConstructor.newInstance(); 

      webappContext.addServlet(servletInstaller.getDisplayName(), servlet).addMapping((urlPattern = servletInstaller.getUrlPattern()) == null ? "/" : urlPattern); 
     } 
     catch (Exception exception) { 
      throw new GrizzlyInitializationException(exception); 
     } 
     } 

     webappContext.deploy(httpServer); 

     for (WebService webService : serviceList) { 

     HttpHandler httpHandler = new JaxwsHandler(webService.getService(), false); 

     httpServer.getServerConfiguration().addHttpHandler(httpHandler, soapPath + webService.getPath()); 
     } 

     try { 
     httpServer.start(); 
     } 
     catch (IOException ioException) { 
     if (!(ioException instanceof BindException)) { 
      throw new GrizzlyInitializationException(ioException); 
     } 
     } 
    } 
    } 

    @Override 
    public void setApplicationContext (ApplicationContext applicationContext) { 

    ExposedApplicationContext.register(applicationContext); 
    } 

    @Override 
    public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException { 

    return bean; 
    } 

    @Override 
    public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException { 

    ServicePath servicePath; 

    if ((servicePath = bean.getClass().getAnnotation(ServicePath.class)) != null) { 
     serviceList.add(new WebService(servicePath.value(), bean)); 
    } 

    return bean; 
    } 

    @Override 
    public synchronized void destroy() { 

    if (httpServer != null) { 
     httpServer.shutdown(); 
    } 
    } 
} 
+0

私はそれを見てみましょう。 –

関連する問題