-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish spring events for query and proxy-method executions
Add a new listener that publishes spring events. Closes #37
- Loading branch information
Showing
11 changed files
with
422 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...rce-micrometer-spring-boot/src/main/java/net/ttddyy/observation/boot/event/JdbcEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.ttddyy.observation.boot.event; | ||
|
||
import org.springframework.context.ApplicationEvent; | ||
|
||
/** | ||
* Base class for JDBC event. | ||
* | ||
* @author Tadaya Tsuyukubo | ||
* @since 1.1 | ||
*/ | ||
public abstract class JdbcEvent extends ApplicationEvent { | ||
|
||
public JdbcEvent(Object source) { | ||
super(source); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...ing-boot/src/main/java/net/ttddyy/observation/boot/event/JdbcEventPublishingListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.ttddyy.observation.boot.event; | ||
|
||
import java.util.List; | ||
|
||
import net.ttddyy.dsproxy.ExecutionInfo; | ||
import net.ttddyy.dsproxy.QueryInfo; | ||
import net.ttddyy.dsproxy.listener.MethodExecutionContext; | ||
import net.ttddyy.dsproxy.listener.MethodExecutionListener; | ||
import net.ttddyy.dsproxy.listener.QueryExecutionListener; | ||
|
||
import org.springframework.context.ApplicationEventPublisher; | ||
|
||
/** | ||
* A datasource-proxy listener that publishes spring events for jdbc interactions. | ||
* | ||
* @author Tadaya Tsuyukubo | ||
* @since 1.1 | ||
*/ | ||
public class JdbcEventPublishingListener implements QueryExecutionListener, MethodExecutionListener { | ||
|
||
private final ApplicationEventPublisher publisher; | ||
|
||
public JdbcEventPublishingListener(ApplicationEventPublisher publisher) { | ||
this.publisher = publisher; | ||
} | ||
|
||
@Override | ||
public void beforeMethod(MethodExecutionContext executionContext) { | ||
this.publisher.publishEvent(new JdbcMethodExecutionEvent(true, executionContext)); | ||
} | ||
|
||
@Override | ||
public void afterMethod(MethodExecutionContext executionContext) { | ||
this.publisher.publishEvent(new JdbcMethodExecutionEvent(false, executionContext)); | ||
} | ||
|
||
@Override | ||
public void beforeQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) { | ||
this.publisher.publishEvent(new JdbcQueryExecutionEvent(true, execInfo, queryInfoList)); | ||
} | ||
|
||
@Override | ||
public void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) { | ||
this.publisher.publishEvent(new JdbcQueryExecutionEvent(false, execInfo, queryInfoList)); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...spring-boot/src/main/java/net/ttddyy/observation/boot/event/JdbcMethodExecutionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.ttddyy.observation.boot.event; | ||
|
||
import net.ttddyy.dsproxy.listener.MethodExecutionContext; | ||
|
||
/** | ||
* An event published when JDBC proxy methods are invoked. | ||
* <p> | ||
* The event is published right before and after executing the proxy methods. | ||
* | ||
* @author Tadaya Tsuyukubo | ||
* @since 1.1 | ||
*/ | ||
public class JdbcMethodExecutionEvent extends JdbcEvent { | ||
|
||
private final boolean before; | ||
|
||
private final MethodExecutionContext executionContext; | ||
|
||
public JdbcMethodExecutionEvent(boolean before, MethodExecutionContext executionContext) { | ||
super(executionContext.getProxy()); | ||
this.before = before; | ||
this.executionContext = executionContext; | ||
} | ||
|
||
/** | ||
* {@code true} when the event is published right before performing the method. | ||
* @return {@code true} if before execution | ||
*/ | ||
public boolean isBefore() { | ||
return this.before; | ||
} | ||
|
||
/** | ||
* {@code true} when the event is published right after performing the method. | ||
* @return {@code true} if after execution | ||
*/ | ||
public boolean isAfter() { | ||
return !this.before; | ||
} | ||
|
||
public MethodExecutionContext getExecutionContext() { | ||
return this.executionContext; | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...-spring-boot/src/main/java/net/ttddyy/observation/boot/event/JdbcQueryExecutionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.ttddyy.observation.boot.event; | ||
|
||
import java.util.List; | ||
|
||
import net.ttddyy.dsproxy.ExecutionInfo; | ||
import net.ttddyy.dsproxy.QueryInfo; | ||
|
||
/** | ||
* An event published when queries are executed. | ||
* <p> | ||
* The event is published right before and after executing the queries. | ||
* | ||
* @author Tadaya Tsuyukubo | ||
* @since 1.1 | ||
*/ | ||
public class JdbcQueryExecutionEvent extends JdbcEvent { | ||
|
||
private final boolean before; | ||
|
||
private final ExecutionInfo execInfo; | ||
|
||
private final List<QueryInfo> queryInfoList; | ||
|
||
public JdbcQueryExecutionEvent(boolean isBefore, ExecutionInfo execInfo, List<QueryInfo> queryInfoList) { | ||
super(execInfo.getStatement()); | ||
this.before = isBefore; | ||
this.execInfo = execInfo; | ||
this.queryInfoList = queryInfoList; | ||
} | ||
|
||
/** | ||
* {@code true} when the event is published right before executing queries. | ||
* @return {@code true} if before execution | ||
*/ | ||
public boolean isBefore() { | ||
return this.before; | ||
} | ||
|
||
/** | ||
* {@code true} when the event is published right after executing queries. | ||
* @return {@code true} if after execution | ||
*/ | ||
public boolean isAfter() { | ||
return !this.before; | ||
} | ||
|
||
public ExecutionInfo getExecInfo() { | ||
return this.execInfo; | ||
} | ||
|
||
public List<QueryInfo> getQueryInfoList() { | ||
return this.queryInfoList; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.