Skip to content

Commit

Permalink
Fix beans-xml issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Oct 1, 2024
1 parent 324de85 commit 02aa818
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 57 deletions.
8 changes: 7 additions & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>3.1.9.Final</version>
<version>4.0.0.Final</version>
</dependency>

<dependency>
Expand All @@ -1167,6 +1167,12 @@
<version>2.5.0</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-cdi2-se</artifactId>
<version>2.28</version>
</dependency>

<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit5</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion dotCMS/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,12 @@
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>3.1.9.Final</version>
<version>4.0.0.Final</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-cdi2-se</artifactId>
</dependency>

<dependency>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
import com.dotcms.analytics.app.AnalyticsApp;
import com.dotcms.analytics.helper.AnalyticsHelper;
import com.dotcms.analytics.model.AccessToken;
import com.dotcms.analytics.model.AccessTokenFetchMode;
import com.dotcms.exception.AnalyticsException;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.google.common.annotations.VisibleForTesting;
import com.liferay.portal.model.User;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Default;

/**
* Factory to create {@link CubeJSClient} instances.
*
* @author vico
*/
@Default
@ApplicationScoped
public class CubeJSClientFactoryImpl implements CubeJSClientFactory {

private static AnalyticsHelper analyticsHelper = AnalyticsHelper.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Default;
import javax.inject.Inject;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class JobQueueProducer {
*
* @return A JobQueue instance
*/
@Produces
@ApplicationScoped
public JobQueue produceJobQueue() {

if (JOB_QUEUE_IMPLEMENTATION_TYPE.equals("postgres")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.dotcms.jobs.business.queue.error.JobNotFoundException;
import com.dotcms.jobs.business.queue.error.JobQueueDataException;
import com.dotcms.jobs.business.queue.error.JobQueueException;
import com.dotcms.repackage.org.directwebremoting.guice.ApplicationScoped;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.common.db.DotConnect;
import com.dotmarketing.exception.DotDataException;
Expand All @@ -28,6 +29,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.enterprise.inject.Default;

/**
* PostgreSQL implementation of the JobQueue interface. This class provides concrete implementations
Expand All @@ -53,6 +55,8 @@
* @see Job
* @see JobState
*/
@Default
@ApplicationScoped
public class PostgresJobQueue implements JobQueue {

private static final String CREATE_JOB_QUEUE_QUERY = "INSERT INTO job_queue "
Expand Down
157 changes: 157 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rest/api/v1/job/JobQueueResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package com.dotcms.rest.api.v1.job;

import com.dotcms.jobs.business.api.JobQueueManagerAPI;
import com.dotcms.jobs.business.job.Job;
import com.dotcms.jobs.business.job.JobPaginatedResult;
import com.dotcms.rest.InitDataObject;
import com.dotcms.rest.ResponseEntityView;
import com.dotcms.rest.WebResource;
import com.dotcms.rest.annotation.NoCache;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.util.Logger;
import com.liferay.portal.model.User;
import org.glassfish.jersey.media.sse.EventOutput;
import org.glassfish.jersey.media.sse.OutboundEvent;
import org.glassfish.jersey.media.sse.SseFeature;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Path("/v1/jobs")
public class JobQueueResource {

private final WebResource webResource;
private final JobQueueManagerAPI jobQueueManagerAPI;

//@Inject
MyTestBean myTestBean;

public JobQueueResource() {
this(new WebResource(), APILocator.getJobQueueManagerAPI());
}

//@Inject
public JobQueueResource(WebResource webResource, JobQueueManagerAPI jobQueueManagerAPI) {
this.webResource = webResource;
this.jobQueueManagerAPI = jobQueueManagerAPI;
}

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createJob(@Context HttpServletRequest request,
@QueryParam("queueName") String queueName,
Map<String, Object> jobParameters) {
try {
InitDataObject initData = webResource.init(null, true, request, true, null);
User user = initData.getUser();

String jobId = jobQueueManagerAPI.createJob(queueName, jobParameters);

return Response.ok(new ResponseEntityView<>(jobId)).build();
} catch (Exception e) {
Logger.error(this, "Error creating job", e);
return Response.serverError().entity(new ResponseEntityView<>(e.getMessage())).build();
}
}

@GET
@Path("/{jobId}/status")
@Produces(MediaType.APPLICATION_JSON)
public Response getJobStatus(@Context HttpServletRequest request, @PathParam("jobId") String jobId) {
try {
InitDataObject initData = webResource.init(null, true, request, true, null);
User user = initData.getUser();

Job job = jobQueueManagerAPI.getJob(jobId);
Map<String, Object> statusInfo = Map.of(
"state", job.state(),
"progress", job.progress(),
"executionNode", job.executionNode().orElse("N/A")
);

return Response.ok(new ResponseEntityView<>(statusInfo)).build();
} catch (Exception e) {
Logger.error(this, "Error getting job status", e);
return Response.serverError().entity(new ResponseEntityView<>(e.getMessage())).build();
}
}

@POST
@Path("/{jobId}/cancel")
@Produces(MediaType.APPLICATION_JSON)
public Response cancelJob(@Context HttpServletRequest request, @PathParam("jobId") String jobId) {
try {
InitDataObject initData = webResource.init(null, true, request, true, null);
User user = initData.getUser();

jobQueueManagerAPI.cancelJob(jobId);
return Response.ok(new ResponseEntityView<>("Job cancelled successfully")).build();
} catch (Exception e) {
Logger.error(this, "Error cancelling job", e);
return Response.serverError().entity(new ResponseEntityView<>(e.getMessage())).build();
}
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response listJobs(@Context HttpServletRequest request,
@QueryParam("page") @DefaultValue("1") int page,
@QueryParam("pageSize") @DefaultValue("20") int pageSize) {
try {
System.out.println(myTestBean.sayHello());
InitDataObject initData = webResource.init(null, true, request, true, null);
User user = initData.getUser();

JobPaginatedResult result = jobQueueManagerAPI.getJobs(page, pageSize);
return Response.ok(new ResponseEntityView(result)).build();
} catch (Exception e) {
Logger.error(this, "Error listing jobs", e);
return Response.serverError().entity(new ResponseEntityView<>(e.getMessage())).build();
}
}

@GET
@Path("/{jobId}/monitor")
@Produces(SseFeature.SERVER_SENT_EVENTS)
@NoCache
public EventOutput monitorJob(@Context HttpServletRequest request, @PathParam("jobId") String jobId) {
EventOutput eventOutput = new EventOutput();
try {
InitDataObject initData = webResource.init(null, true, request, true, null);
User user = initData.getUser();

jobQueueManagerAPI.watchJob(jobId, job -> {
try {
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
eventBuilder.name("job-update");
eventBuilder.data(Job.class, job);
eventOutput.write(eventBuilder.build());
} catch (IOException e) {
Logger.error(this, "Error writing SSE event", e);
}
});

// Keep the connection open for a reasonable time (e.g., 5 minutes)
if (!eventOutput.isClosed()) {
Thread.sleep(TimeUnit.MINUTES.toMillis(5));
}
} catch (Exception e) {
Logger.error(this, "Error monitoring job", e);
} finally {
try {
eventOutput.close();
} catch (IOException e) {
Logger.error(this, "Error closing SSE connection", e);
}
}
return eventOutput;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dotcms.rest.api.v1.job;

public interface MyTestBean {

String sayHello();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dotcms.rest.api.v1.job;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class MyTestBeanImpl implements MyTestBean {

public String sayHello() {
return "Hello, World!";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
public class DotRestApplication extends ResourceConfig {

public DotRestApplication() {

register(MultiPartFeature.class).
register(JacksonJaxbJsonProvider.class).
registerClasses(customClasses.keySet()).
Expand Down
12 changes: 8 additions & 4 deletions dotCMS/src/main/webapp/WEB-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee"
version="2.0" bean-discovery-mode="annotated">
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0" bean-discovery-mode="all">

<scan>
<exclude name="org.apache.**"/>
<exclude name="io.lettuce.**"/>
<exclude name="org.glassfish.**"/>
</scan>
</beans>

</beans>
10 changes: 6 additions & 4 deletions dotCMS/src/test/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee"
version="2.0" bean-discovery-mode="annotated">
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0" bean-discovery-mode="all">

<scan>
<exclude name="org.apache.**"/>
</scan>
</beans>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import com.dotcms.jobs.business.api.events.EventProducer;
import com.dotcms.jobs.business.api.events.RealTimeJobMonitor;
import com.dotcms.jobs.business.error.CircuitBreaker;
import com.dotcms.jobs.business.error.ExponentialBackoffRetryStrategy;
import com.dotcms.jobs.business.error.RetryStrategy;
import com.dotcms.jobs.business.error.RetryStrategyProducer;
import com.dotcms.jobs.business.queue.JobQueue;
import com.dotcms.jobs.business.queue.JobQueueProducer;
import javax.inject.Inject;
import org.jboss.weld.bootstrap.api.helpers.RegistrySingletonProvider;
import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldJunit5Extension;
import org.jboss.weld.junit5.WeldSetup;
Expand All @@ -29,15 +22,7 @@
public class JobQueueManagerAPICDITest {

@WeldSetup
public WeldInitiator weld = WeldInitiator.of(
WeldInitiator.createWeld()
.containerId(RegistrySingletonProvider.STATIC_INSTANCE)
.beanClasses(JobQueueManagerAPIImpl.class, JobQueueConfig.class,
JobQueue.class, RetryStrategy.class, CircuitBreaker.class,
JobQueueProducer.class, JobQueueConfigProducer.class,
RetryStrategyProducer.class, RealTimeJobMonitor.class,
EventProducer.class)
);
public WeldInitiator weld = WeldInitiator.performDefaultDiscovery();

@Inject
private JobQueueManagerAPI jobQueueManagerAPI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void init() throws Exception {
if (initCompleted.compareAndSet(false, true)) {

weld = new Weld().containerId(RegistrySingletonProvider.STATIC_INSTANCE)
/*
.beanClasses(
JobQueueManagerAPIImpl.class,
JobQueueConfig.class,
Expand All @@ -71,6 +72,7 @@ public void init() throws Exception {
RetryStrategyProducer.class,
RealTimeJobMonitor.class,
EventProducer.class)
*/
.initialize();

System.setProperty(TestUtil.DOTCMS_INTEGRATION_TEST, TestUtil.DOTCMS_INTEGRATION_TEST);
Expand Down
Loading

0 comments on commit 02aa818

Please sign in to comment.