Skip to content

Instantly share code, notes, and snippets.

@abeykoon
abeykoon / ballerina-retry-func-with-delay.bal
Created November 13, 2021 07:18
Retry a test function with a time interval
import ballerina/io;
import ballerina/lang.runtime;
import ballerina/log;
public function main() returns error? {
_ = check executeWithRetry(myTest, 3, 5, "this is an error");
}
# Executes a given function with retry. If there is no error the function will
# immediately return. If there's errors it will retry as per given parameters.
import ballerina/http;
import ballerinax/mysql.driver as _; //as we do not use it in the code
import ballerinax/mysql;
import ballerina/sql;
import ballerina/io;
final mysql:Client mysqlClient = check new (user = "root",
password = "root", database = "hospital_data");
service /hospitalservice on new http:Listener(9090) {
<!-- ================================================= -->
<!-- Message Builders -->
<!-- ================================================= -->
<messageBuilder contentType="application/file"
class="org.apache.axis2.format.BinaryBuilder"/>
<!-- ================================================= -->
<!-- Message Formatters -->
<!-- ================================================= -->
<messageFormatter contentType="application/file"
<?xml version="1.0" encoding="UTF-8"?>
<endpoint name="TestHTTPEP" xmlns="http://ws.apache.org/ns/synapse">
<http method="post" uri-template="http://localhost:9000/sendfile">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1.0</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="FileStreamingSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<log description="log" level="custom">
<property name="message" value="File streaming invoked"/>
</log>
<fileconnector.read>
<source>/Users/hasitha/temp/file-streaming/sourceFile</source>
<contentType>application/file</contentType>
<filePattern>.*\.mkv</filePattern>
<streaming>true</streaming>
<?xml version="1.0" encoding="UTF-8"?>
<api context="/streamfile" name="StreamFileInvokeAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<sequence key="FileStreamingSeq"/>
<respond description="respond back"/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
@abeykoon
abeykoon / File-streaming-handler.java
Last active August 5, 2020 12:20
How to stream bytes received from socket to a file
InputStream requestStream = httpExchange.getRequestBody();
File targetFile = new File("/Users/hasitha/temp/file-streaming/targetFile/targetFile.mkv");
OutputStream outStream = null;
try {
outStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = requestStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
@abeykoon
abeykoon / ProbeAPI.xml
Last active January 27, 2020 02:49
WSO2-MI-Probe
<?xml version="1.0" encoding="UTF-8"?>
<api context="/healthz" name="ProbeAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET">
<inSequence>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
<?xml version="1.0" encoding="UTF-8"?>
<api context="/testapi" name="testAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST GET">
<inSequence>
<log>
<property name="message" value="API HIT!!"/>
</log>
<property expression="wso2:vault-lookup('mypassword1')" name="testProperty" scope="default" type="STRING"/>
<log level="custom">
<property expression="$ctx:testProperty" name="secure valut log"/>
FROM docker.wso2.com/wso2mi:latest
COPY CarbonProbeCompositeApplication_1.0.0.car /home/wso2carbon/wso2mi-1.1.0/repository/deployment/server/carbonapps
COPY testProjectCompositeApplication_1.0.0.car /home/wso2carbon/wso2mi-1.1.0/repository/deployment/server/carbonapps
COPY --chown=wso2carbon:wso2 secure-vault.properties /home/wso2carbon/wso2mi-1.1.0/registry/config/repository/components/secure-vault/
COPY --chown=wso2carbon:wso2 secret-conf.properties /home/wso2carbon/wso2mi-1.1.0/conf/security
COPY --chown=wso2carbon:wso2 cipher-tool.properties /home/wso2carbon/wso2mi-1.1.0/conf/security
COPY --chown=wso2carbon:wso2 password-tmp /home/wso2carbon/wso2mi-1.1.0