簡體   English   中英

Tomcat:以編程方式遠程部署服務

[英]Tomcat: Remotely deploy service programmatically

我正在嘗試提供一項將在雄貓服務器上部署WAR文件的服務。 該類將輸入作為WAR文件的路徑,並在運行以下代碼后將其自動部署到tomcat服務器。 請幫助我。 我也不想使用SFTP上傳WAR,而是使用put請求將WAR文件部署在tomcat上。 我已經看到了使用FTP或SFTP的示例,但這不是我所需要的。

public class DeployManager {

static CredentialsProvider credsProvider = new BasicCredentialsProvider();;

public static void main(String args[]) throws ClientProtocolException, IOException {
    /*
     * warning only ever AuthScope.ANY while debugging with these settings
     * the tomcat username and pw are added to EVERY request
     */

    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("tomcat", "s3cret"));
    deploy();
    // undeploy();
}

private static void deploy() throws ClientProtocolException, IOException {
    String url = "http://localhost:8080/manager/text/deploy?path=C:/Users/Jainesh_Trivedi/Desktop/WAR/AutohostDemo1_1145.war";
    File file = new File("C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war");

    HttpPut req = new HttpPut(url);
    MultipartEntityBuilder meb = MultipartEntityBuilder.create();
    meb.addTextBody("Sample", "C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war");

    // "application/octect-stream"
    meb.addBinaryBody("attachment", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());

    req.setEntity(meb.build());
    String response = executeRequest(req, credsProvider);

    System.out.println("Response : " + response);
}

public static void undeploy() throws ClientProtocolException, IOException {
    String url = "http://localhost:8080/manager/text/undeploy?path=/deployMe";
    HttpGet req = new HttpGet(url);
    String response = executeRequest(req, credsProvider);
    System.out.println("Response : " + response);
}

private static String executeRequest(HttpRequestBase requestBase, CredentialsProvider credsProvider)
        throws ClientProtocolException, IOException {
    CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    InputStream responseStream = null;
    String res = null;
    HttpResponse response = client.execute(requestBase);
    HttpEntity responseEntity = response.getEntity();
    responseStream = responseEntity.getContent();

    BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
        sb.append(System.getProperty("line.separator"));
    }
    br.close();
    res = sb.toString();

    return res;
}
}

錯誤是:

INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error
Nov 24, 2015 10:39:23 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request
Exception in thread "main" java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:117)
    at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:129)
    at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:158)
    at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:114)
    at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:120)
    at org.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:150)
    at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:173)
    at org.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:97)
    at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:156)
    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.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)
    at com.sun.proxy.$Proxy0.sendRequestEntity(Unknown Source)
    at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:177)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:77)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:95)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at com.autohost.java.DeployManager.executeRequest(DeployManager.java:68)
    at com.autohost.java.DeployManager.deploy(DeployManager.java:51)
    at com.autohost.java.DeployManager.main(DeployManager.java:35)

您的目標是在Tomcat上部署戰爭。 我建議使用Maven。 如果您已經在項目中使用過maven,請使用tomcat插件。 它將為您制造戰爭並將其部署在tomcat上。

假設您正在使用Tomcat7。在pom.xml的<build>標記中添加以下內容。

     <build>
        <finalName>AutohostDemo1_1145</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>TomcatServer</server>
                    <path>/AutohostDemo1_1145</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

保持tomcat運行。 從存儲庫中獲取所有最新代碼。 然后從項目的根目錄(包含pom.xml文件的目錄)中運行以下命令。

mvn tomcat7:redeploy

此命令將創建一個war文件並將其部署到tomcat。 我們甚至不需要手動重新啟動tomcat。

如果要在遠程服務器上執行此操作,則可以在服務器上克隆一次存儲庫。 之后,每當您需要在tomcat上部署新戰爭時,您都可以使用telnet連接並從項目的根目錄(在遠程服務器上)運行相同的命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM