How to Add an Attachment to a Task with the jBPM Human Task API

import static java.lang.System.out;

import java.util.Date;

import org.drools.SystemEventListenerFactory;
import org.jbpm.task.AccessType;
import org.jbpm.task.Attachment;
import org.jbpm.task.Content;
import org.jbpm.task.User;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.mina.MinaTaskClientConnector;
import org.jbpm.task.service.mina.MinaTaskClientHandler;
import org.jbpm.task.service.responsehandlers.BlockingAddAttachmentResponseHandler;

public final class JbpmSample {

  public static void main(final String[] args) throws Exception {
    final TaskClient client = new TaskClient(new MinaTaskClientConnector("SampleClient", new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

    if (client.connect("localhost", 9123)) {
      final BlockingAddAttachmentResponseHandler handler = new BlockingAddAttachmentResponseHandler();

      final Attachment attachment = new Attachment();

      attachment.setAccessType(AccessType.Inline); // required

      attachment.setAttachedAt(new Date(System.currentTimeMillis())); // required

      attachment.setAttachedBy(new User("userId")); // required

      attachment.setContentType(""); // required

      attachment.setName(""); // required

      final Content content = new Content();

      content.setContent(new byte[] {}); // required

      client.addAttachment(1, attachment, content, handler);

      out.println(handler.getAttachmentId());

      client.disconnect();
    }

  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s