我已经编写了一些代码来在代码运行时创建文件。我的计划是定期将其放在Microsoft Azure功能应用程序上,因此它每小时或每天运行一次。我将使用timertrigger并在其中也有HTTP请求触发器进行测试,但是,我不知道在哪里放置自己的代码来启动和运行它以及外部Java库。
创建函数app work并与maven一起运行时给出的基本代码,但是当我创建另一个函数并将自己的代码放入其中时会崩溃,并说我使用的外部java库有很多错误, JSON,JSON简单和Apache Commons。
我要寻找的最终结果是能够放入我的代码并使程序按计划运行并创建我想要的文件。现在,无论在哪里插入代码,我都会遇到错误。
更新:我认为问题可能出在我使用的外部库上,但我不知道它们为什么会引起问题或如何解决。我需要外部库来使代码正常工作。我将它们添加到项目的构建路径中,以便它们可以正常工作。 对于所有导入的库,它给我错误“包org.apache.commons.io不存在”之类的错误
答案 0 :(得分:1)
这是Azure functions Java project的文件夹结构:
from kivy.app import App
from kivy.graphics.vertex_instructions import Line
from kivy.lang import Builder
from kivy.uix.widget import Widget
class MyPaintWidget(Widget):
def __init__(self, **kwargs):
super(MyPaintWidget, self).__init__(**kwargs)
self.circle_center = None
def draw_arc(self, *args):
if len(args) > 0:
# this is the result of a touch event
if not self.collide_point(*args[0].pos):
# touch was not within the MyPaintWidget
return
else:
# set circle center to touch position
self.circle_center = args[0].pos
if self.circle_center is None:
# default value for circle center
self.circle_center = self.center
# get the root widget (the FloatLayout)
root = App.get_running_app().root
# get the canvas of the MyPaintWidget
can = root.ids.mypaintwidget.canvas
# clear any previous drawing and draw a new arc
can.clear()
with can:
Line(circle=(self.circle_center[0], self.circle_center[1], root.ids.radius.value, 0.0, root.ids.arc.value))
theRoot = Builder.load_string('''
FloatLayout:
MyPaintWidget:
id: mypaintwidget
pos_hint: {'x':0.0, 'top':1.0}
size_hint: 0.75, 1.0
on_touch_down:
self.draw_arc(args[1])
BoxLayout:
pos_hint: {'right':1.0, 'top':1.0}
size_hint: 0.25, 1.0
orientation: 'vertical'
Slider:
id:radius
min: 0.
max: 50.
step:1
value: 50
value_track_color:1,0,0,1
on_value: mypaintwidget.draw_arc()
Label:
color:1,0,0,1
pos:root.pos
text: 'radius = {}'.format(radius.value)
Slider:
id: arc
min:0.
max: 360.
value:270
step:1
value_track_color:1,0,0,1
on_value: mypaintwidget.draw_arc()
Label:
color:1,0,0,1
pos:root.pos
text: 'arc = {}'.format(arc.value)
''')
class ArcDrawApp(App):
def build(self):
return theRoot
ArcDrawApp().run()
目标目录中的FunctionApp是在Azure中部署到您的Function App的内容。 Java库应该位于lib文件夹中。
您可以参考此guide来使用Java和Maven部署功能。
答案 1 :(得分:1)
您可以遵循@Caiyi提供的指南,也可以遵循我过去所做的一些详细步骤。
例如,我想在我的Java函数应用程序中使用Azure blob存储sdk。
功能类:
package com.fabrikam.functions;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.ExecutionContext;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
/**
* Hello function with HTTP Trigger.
*/
public class Function {
// Configure the connection-string with your values
public static final String storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName=***;" +
"AccountKey=***";
@FunctionName("hello")
public String hello(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) String req,
ExecutionContext context) {
try {
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container.
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference(req);
// Create the container if it does not exist.
container.createIfNotExists();
return String.format("Hello, I get container name : %s!", container.getName());
} catch (Exception e) {
// Output the stack trace.
e.printStackTrace();
return "Access Error!";
}
}
}
Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.fabrikam.functions</groupId>
<artifactId>fabrikam-functions</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Azure Java Functions</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<functionAppName>fabrikam-functions-20171017112209094</functionAppName>
</properties>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-java-core</artifactId>
<version>1.0.0-beta-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>6.0.0</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>0.1.4</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<configuration>
<resourceGroup>java-functions-group</resourceGroup>
<appName>${functionAppName}</appName>
<region>westus2</region>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>beta</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/azure-functions/${functionAppName}
</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>host.json</include>
<include>local.settings.json</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后使用命令mvn clean package
将您的Maven项目打包到jar包中。
使用命令mvn azure-functions:run
在本地运行Azure函数。
现在,如果您运行Azure功能,则可能会看到以下问题。
java.lang.NoClassDefFoundError: com / microsoft / azure / storage / CloudStorageAccount
Exception:
Stack: java.lang.reflect.InvocationTargetException
[10/25/2017 2:48:44 AM] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[10/25/2017 2:48:44 AM] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[10/25/2017 2:48:44 AM] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[10/25/2017 2:48:44 AM] at java.lang.reflect.Method.invoke(Method.java:498)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:19)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:34)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:40)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.MessageHandler.handle(MessageHandler.java:41)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:84)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1386)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
[10/25/2017 2:48:44 AM] Caused by: java.lang.NoClassDefFoundError: com/microsoft/azure/storage/CloudStorageAccount
[10/25/2017 2:48:44 AM] at com.fabrikam.functions.Function.hello(Function.java:26)
[10/25/2017 2:48:44 AM] ... 16 more
[10/25/2017 2:48:44 AM] Caused by: java.lang.ClassNotFoundException: com.microsoft.azure.storage.CloudStorageAccount
[10/25/2017 2:48:44 AM] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[10/25/2017 2:48:44 AM] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[10/25/2017 2:48:44 AM] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[10/25/2017 2:48:44 AM] ... 17 more
[10/25/2017 2:48:44 AM] .
[10/25/2017 2:48:44 AM] Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '3450abda-99a0-4d75-add2-a7bc48a0cb51'
[10/25/2017 2:48:44 AM] System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result:
这是因为该罐子包装时没有dependent jar packages
。
因此,请将以下配置摘要添加到我的pom.xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Your main class path</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
然后使用命令mvn-clean-package
,您将看到生成了两个jar文件。
第一个是它不包含dependent jar packages
,第二个是包含dependent jar packages
。
将fabrikam-functions-1.0-SNAPSHOT-jar-with-dependencies
罐子移到路径:${project.basedir}/target/azure-functions/${function-app-name}/
对我来说,它看起来像E:\TestAzureFunction\fabrikam-functions\target\azure-functions\fabrikam-functions-20171017112209094
。
不要忘记将罐子重命名为fabrikam-functions-1.0-SNAPSHOT
。
最后,成功运行azure函数,并通过以下网址获取输出结果:http://localhost:7071/api/hello
。
此外,您可以参考此github doc以获得有关天蓝色function maven plugin
的更多配置详细信息。