Use only for writing files --Apache Camel

theme

Even if you don't use Apache Camel (system linkage), it's convenient just to use a convenient library, so try using it.

However, the "text file output" in the example written here is easy without installing a library. .. ..

I think.

I think that you can use various functions by just changing the options, so please try it.

Output file

-Output the character string "aaaa" to a file -Output file name "test / a4.txt" ・ If there is no test folder, it will be created automatically. -File is overwritten


import org.apache.camel.*;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.main.Main;

public class FileTest {

    public static Main camel = new Main();

    public static void main(String[] args) throws Exception {
        camel.start();
        ProducerTemplate producer = camel.getCamelTemplate();

        //Data creation
        Exchange exchange = new DefaultExchange(producer.getCamelContext());
        Message message = exchange.getIn();
        message.setHeader(Exchange.FILE_NAME, "test/a4.txt");
        message.setBody("aaaa"); //Output content is text

        //File output
        String directory = ".";
        Exchange result = producer.send("file://" + directory, exchange);

        //Error checking
        if (result.getException() != null)
            throw result.getException();
    }

}

pom.xml part


        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel-ver}</version>
        </dependency>

File output (file addition)

Rewrite the options below

Exchange result = producer.send("file://" + directory + "?fileExist=Append", exchange);

File output (error if file exists)

Rewrite the options below

Exchange result = producer.send("file://" + directory + "?fileExist=Fail", exchange);

File output (copy of file)

Rewrite the following implementation. filename.txt is copied to a4.txt The content is a copy and the file name is test / a4.txt.

message.setHeader(Exchange.FILE_NAME, "test/a4.txt");
message.setBody(new File("filename.txt"));

File output (copy file. Copy file modification date)

Rewrite the following implementation. filename.txt is copied to a4.txt

File file = new File("bbb.txt");
message.setBody(file);
message.setHeader(Exchange.FILE_LAST_MODIFIED, file.lastModified());
Exchange result = producer.send("file://" + directory + "?keepLastModified=true", exchange);

File output (copy file. Copy file modification date, permission is 777)

Rewrite the following implementation. filename.txt is copied to a4.txt

File file = new File("bbb.txt");
message.setBody(file);
message.setHeader(Exchange.FILE_LAST_MODIFIED, file.lastModified());
Exchange result = producer.send("file://" + directory + "?keepLastModified=true&chmod=777", exchange);

For other options http://camel.apache.org/file2.html

HTTP (S) access

-The result of accessing by HTTPS is entered in the response


import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.main.Main;

public class HttpTest {
    public static Main camel = new Main();

    public static void main(String[] args) throws Exception {
        camel.start();
        ProducerTemplate producer = camel.getCamelTemplate();

        //Data creation
        Exchange exchange = new DefaultExchange(producer.getCamelContext());
        Message message = exchange.getIn();

        //HTTP access
        Exchange result = producer.send("https://www.google.co.jp", exchange);

        //Error checking
        if (result.getException() != null)
            throw result.getException();

        //Get results
        String response = result.getOut().getBody(String.class);
        System.out.println(response);
    }
}

pom.Part of xml


        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http</artifactId>
            <version>${camel-ver}</version>
        </dependency>

Http access (Basic authentication)

I will rewrite it as follows

        Exchange result = producer.send("http://192.168.33.10/member/bbb.html?authMethod=Basic&authUsername=secret&authPassword=aaa", exchange);

Recommended Posts

Use only for writing files --Apache Camel
[Java] What should I use for writing files?
[Apache Camel] Use File component
Item 69: Use exceptions only for exceptional conditions
How to use binding.pry for view files
Use Maven only for downloading dependent libraries
Ruby Learning # 27 Writing Files