通过MuleSoft使用Salesforce REST服务

时间:2015-03-20 15:39:19

标签: rest salesforce mule

我想要使用Salesforce公开的REST服务,现在我使用Sandbox实例并使用Anypoint Studio创建我的流程,我想知道实现它的最佳实践。现在我不确定salesforce连接器是否允许我使用该服务,因此,我使用这些组件:

这是全流程。

<mule xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:oauth2="http://www.mulesoft.org/schema/mule/oauth2" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.6.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd">
    <http:request-config name="HTTP_Request_Configuration" host="https://test.salesforce.com" port="80"  doc:name="HTTP Request Configuration" protocol="HTTPS">
        <oauth2:client-credentials-grant-type clientId="3xQYwWqsvZUYbwMRLHiXQy9A23XXXtxg1OMxdXh5p4eoev53q26CmN0Yx0EyLf_HTLW9X" clientSecret="710XXX1723685311">
            <oauth2:token-request tokenUrl="https://test.salesforce.com/services/oauth2/token"/>
        </oauth2:client-credentials-grant-type>
    </http:request-config>
    <file:connector name="File" writeToDirectory="/Users/emoran/Desktop" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
    <flow name="outbrain_3wintegrationFlow1">
        <poll doc:name="Poll">
            <fixed-frequency-scheduler frequency="10" timeUnit="SECONDS"/>
            <http:request config-ref="HTTP_Request_Configuration" path="services/apexrest/outbrain_integration_messages/" method="GET" requestStreamingMode="ALWAYS" sendBodyMode="ALWAYS"  doc:name="Consume SFDC LOG Service" doc:description="prueba" parseResponse="false" source="#[message.payload]" target="#[message.payload]">
                <http:request-builder>
                </http:request-builder>
                <http:success-status-code-validator values="0..599"/>
            </http:request>
        </poll>
        <logger message="&gt;&gt;&gt;&gt; RESPONSE FROM SERVER:  #[message.payload]" level="DEBUG" doc:name="Logger"/>
        <echo-component doc:name="Echo"/>
        <file:outbound-endpoint path="/" outputPattern="refacciones.json" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

使用此服务的最佳方法是什么?

埃德加,

2 个答案:

答案 0 :(得分:1)

Mule中的Salesforce连接器允许您使用Salesforce公开的所有服务。您应该在轮询范围内使用Salesforce连接器从Salesforce获取数据。

您需求的简单示例如下所示。

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:context="http://www.springframework.org/schema/context" xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
    <context:property-placeholder location="file:${mule_home}/conf/salesforce-config.properties"/>
    <sfdc:config name="Salesforce" doc:name="Salesforce" password="${sf_password}" securityToken="${sf_security_token}" url="${sf_url}" username="${sf_username}">
        <sfdc:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW"/>
    </sfdc:config>
    <flow name="sfdcpollerFlow1" doc:name="sfdcpollerFlow1">
        <poll doc:name="Poll">
            <sfdc:get-updated config-ref="Salesforce"  doc:name="Salesforce" duration="10" type="Contract"/>
        </poll>
        <logger message="RESPONSE FROM SERVER:  #[payload]" level="INFO" doc:name="Logger"/>
        <echo-component doc:name="Echo"/>
        <file:outbound-endpoint path="${file_path}" outputPattern="refacciones.json" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>
&#13;
&#13;
&#13;

在上面的示例中,我将获取已修改的联系人并将其放置到文件夹中。您可以参考mule软文档以获取有关Salesforce连接器的更多详细信息。

http://www.mulesoft.org/documentation/display/current/Salesforce+Connector http://www.mulesoft.org/documentation/display/current/Connect+with+Salesforce+Example

答案 1 :(得分:1)

使用Mule中的HTTP连接器与Salsforce的Rest API进行交互是完全有效的。但是,正如其他人提到的那样,在处理Salesforce API(包括身份验证和连接管理等)时,Salesforce连接器会大量提高复杂性。

我注意到您的示例使用OAuth,其他示例使用基本/ ws安全性,但Salesforce连接器也支持OAuth:http://www.mulesoft.org/documentation/display/current/Salesforce+Connector+Authentication

然而,在引擎盖下,Salesforce连接器主要使用Salesforce SOAP API。因此,如果您特别需要使用Rest API,则连接器可能无法提供帮助。