如何使用Java与Eclipse和Maven一起创建Azure webjob?
我想使用Java创建一个计划的webjob,每天归档一次数据。我的计划是尽量减少webjob本身的依赖性,只需对我的主应用程序的运行实例进行Http调用,这样它就可以处理其余部分。我遇到的问题是,在成功上传webjob之后,我单击run,它成功了,然后立即失败。只记录了成功,所以我完全不知道是什么导致了失败。我希望有更多经验的人能带我走过这个过程,帮助我找出我缺少的东西 Java代码如下所示:如何使用Java与Eclipse和Maven一起创建Azure webjob?,java,eclipse,azure,azure-webjobs,azure-webapps,Java,Eclipse,Azure,Azure Webjobs,Azure Webapps,我想使用Java创建一个计划的webjob,每天归档一次数据。我的计划是尽量减少webjob本身的依赖性,只需对我的主应用程序的运行实例进行Http调用,这样它就可以处理其余部分。我遇到的问题是,在成功上传webjob之后,我单击run,它成功了,然后立即失败。只记录了成功,所以我完全不知道是什么导致了失败。我希望有更多经验的人能带我走过这个过程,帮助我找出我缺少的东西 Java代码如下所示: HttpURLConnection httpConnection = null;
HttpURLConnection httpConnection = null;
Integer responseCode = null;
try {
URL serverUrl = new URL(endpoint);
httpConnection = (HttpURLConnection) serverUrl.openConnection();
httpConnection.setRequestMethod("GET");
responseCode = httpConnection.getResponseCode();
System.out.println("Status: " + responseCode + " - " + httpConnection.getResponseMessage());
return responseCode;
} catch (Exception e) {
if (responseCode != null) {
System.out.println("Status: " + responseCode + " - " + httpConnection.getResponseMessage());
}
e.printStackTrace();
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
return 400;
简化,但只需知道它自己运行得非常好
我的过程是将此代码导出为jar文件,然后将jar文件和批处理文件一起压缩到zip中
批处理文件的内容如下:
set JAVA_HOME=D:\Program Files (x86)\Java\jdk1.8.0_202
set CLASSPATH=.;%JAVA_HOME%\lib
set PATH=%JAVA_HOME%\bin;%PATH%
java -jar archive-webjob.jar
我已经尝试过使用不同的JDK,1.8.0_202是最新的
在其他地方,我看到其他文件,如web.config或settings.job正在使用,但我自己在使用它们时没有看到任何区别
至于我在Azure上的设置,always on设置为true,Tomcat 8.5(自动)、Java 1.8(自动)、64位和最低安全配置也设置为true
不幸的是,我很难找到关于如何做到这一点的资源。所以,如果我遗漏了一些明显的东西,请容忍我
仅供参考,不幸的是,Azure功能对我来说已不再适用,因为我的公司不允许使用Azure CLI进行部署,尽管我相信这将是一个很好的解决方案
提前感谢您提供的有用信息。因此,稍后的日志确实详细说明了故障。问题是我压缩了错误的jar文件(我在eclipse上手动导出的文件),该文件在MANIFEST.MF
文件中没有“Main Class”属性。为了避免任何人在这段时间遇到困难,我将继续并清楚地概述我所采取的步骤
HttpURLConnection httpConnection = null;
Integer responseCode = null;
try {
URL serverUrl = new URL(endpoint);
httpConnection = (HttpURLConnection) serverUrl.openConnection();
httpConnection.setRequestMethod("GET");
responseCode = httpConnection.getResponseCode();
System.out.println("Status: " + responseCode + " - " + httpConnection.getResponseMessage());
return responseCode;
} catch (Exception e) {
if (responseCode != null) {
System.out.println("Status: " + responseCode + " - " + httpConnection.getResponseMessage());
}
e.printStackTrace();
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
return 400;
“jar with dependencies”后缀是以下原因造成的:
mvn clean package
将在目标文件夹中生成一个带有dependencies.jar的AppName版本jar(无需手动导出为jar),该jar将有一个详细的MANIFEST.MF
文件,其中包括这次的主类属性
AppName版本jar与run.bat
文件一起压缩到.zip中
希望这能节省一些时间。如果我没有记录下我一天半的时间试图弄明白这一点,我会感到内疚 那么,上面的代码有什么问题呢?好的问题,最后我自己解决了,并在下面给出了答案。无论如何,谢谢你。
...
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.otherPackages.MainClassName</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Manifest-Version: 1.0
Main-Class: com.otherPackages.MainClassName