forked from sermakov/JavaPatternMirea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsk22.java
More file actions
260 lines (210 loc) · 9.01 KB
/
tsk22.java
File metadata and controls
260 lines (210 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package a;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import javax.management.ObjectName;
import javax.persistence.*;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileWriter;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.util.*;
import java.util.stream.Stream;
import static java.util.Objects.requireNonNull;
/*
sudo mkdir /run/postgresql
sudo chgrp postgres /run/postgresql
sudo chown postgres /run/postgresql
sudo su - postgres
pg_ctl -D /var/lib/postgres/data start
psql -d db
select * from urs;
select * from dgs;
pg_ctl -D /var/lib/postgres/data stop
management.endpoints.web.exposure.include=usr,dg
*/
@SpringBootApplication @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
public class tsk22 {
private static final String db = "db";
private static final String tb1 = "urs";
private static final String tb2 = "dgs";
@SneakyThrows
public static void main(String[] args) {
val a = new SpringApplication(tsk22.class);
a.setDefaultProperties(
Collections.singletonMap("server.port", 8081));
val b = a.run(args);
ManagementFactory
.getPlatformMBeanServer()
.registerMBean(
new nb(b.getBean(sch.class)),
new ObjectName(tsk22.class.getName() + ":a=b"));
}
////////////////////////////////////////////////////////////////////// management
public interface nbMBean { void wd(); }
public record nb(sch sc) implements nbMBean
{ @Override public void wd() { sc.a(); } }
////////////////////////////////////////////////////////////////////// configuration
@EnableAspectJAutoProxy @Slf4j @EnableAsync @EnableScheduling
@EnableJpaRepositories(considerNestedRepositories = true)
@Configuration public static class A {
@Bean public DataSource ds() {
val a = new HikariConfig();
a.setJdbcUrl("jdbc:postgresql://localhost:5432/" + db);
a.setDriverClassName("org.postgresql.Driver");
a.setUsername("postgres");
a.setPassword("postgres");
return new HikariDataSource(a);
}
@Bean(name = "entityManagerFactory")
public LocalSessionFactoryBean fc(DataSource b) {
val a = new LocalSessionFactoryBean();
a.setDataSource(b);
a.setPackagesToScan(this.getClass().getPackageName());
val c = new Properties();
c.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQL92Dialect");
c.setProperty("hibernate.show_sql", "true");
c.setProperty("hibernate.hbm2ddl.auto", "update");
a.setHibernateProperties(c);
return a;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager
pt(LocalSessionFactoryBean a) {
return new HibernateTransactionManager(
requireNonNull(a.getObject())); }
}
@Aspect @Slf4j @Component
public static class asp {
@Before("within(a.tsk22$asv+)")
public void lg(JoinPoint a) { log.info(
"proxy logging %s".formatted(a.toString())
); }
}
////////////////////////////////////////////////////////////////////// service
@Service public static class sch {
@Autowired private ussi uu;
@Autowired private dgsi dd;
@Scheduled(fixedRate = 30 * 60 * 60 * 1000)
@Async @SneakyThrows public void a() {
val b = new File("/home/admin/IdeaProjects/sprBoot/src/main/resources/aa");
assert b.exists() && b.isDirectory();
Arrays.stream(requireNonNull(b.listFiles()))
.map(File::delete).close();
Stream.of(usr.class, dg.class).forEach(d -> { try {
System.out.println("gyhbymk " + d);
val e = d.getSimpleName();
val f = new File(b, e);
assert f.createNewFile();
val g = new FileWriter(f);
g.write((
d.equals(usr.class) ?
uu : dd
).gt());
g.flush();
g.close();
} catch (Exception ignored) {} });
}
}
@SuppressWarnings("unused") private interface $$<T extends _$, R extends $$$$<T>>
{ List<T> gal(); void dd(T a); void dl(int a); T nw(T a); String gt(); }
public interface uss extends $$<usr, usp> {}
public interface dgs extends $$<dg, dgp> {}
@RequiredArgsConstructor public abstract static class
asv<T extends _$, R extends $$$$<T>> implements $$<T, R> {
private final R rp;
@Transactional(readOnly = true) @Override
public List<T> gal() { return rp.findAll(); }
@Transactional @Override public void dd(T a) { rp.save(a); }
@Transactional @Override public void dl(int a) { rp.deleteById(a); }
@Override public T nw(T a) { dd(a); return a; }
@Override public String gt() { return gal().toString(); }
}
// GET curl http://localhost:8081/actuator/usr
@Endpoint(id = "usr") @Service
public static class ussi extends asv<usr, usp> implements uss {
public ussi(usp a) { super(a); }
@ReadOperation public String a() { return gt(); }
// curl -X POST -d '{"a":"a","b":"b"}' -H \
// "Content-type: application/json" 'http://localhost:8081/actuator/usr'
@WriteOperation public String b(String a, String b)
{ return nw(new usr(a, b)).toString(); }
}
// GET curl http://localhost:8081/actuator/dg
@Endpoint(id = "dg") @Service
public static class dgsi extends asv<dg, dgp> implements dgs {
public dgsi(dgp a) { super(a); }
@ReadOperation public String a() { return gt(); }
// curl -X POST -d '{"a":"a","b":"b"}' -H \
// "Content-type: application/json" 'http://localhost:8081/actuator/dg'
@WriteOperation public String b(String a, String b)
{ return nw(new dg(a, b)).toString(); }
}
////////////////////////////////////////////////////////////////////// repository
@NoRepositoryBean public interface
$$$$<T extends _$> extends JpaRepository<T, Integer> {}
@Repository public interface usp extends $$$$<usr> {}
@Repository public interface dgp extends $$$$<dg> {}
////////////////////////////////////////////////////////////////////// data
@MappedSuperclass
public static abstract class _$ implements Serializable
{ @GeneratedValue(strategy = GenerationType.IDENTITY)
@Nullable @Id Integer id; }
@Table(name = tb1) @Entity
@RequiredArgsConstructor
public static class usr extends _$ {
@NonNull String fn;
@NonNull String ln;
@OneToMany(
mappedBy = "rs",
fetch = FetchType.EAGER)
List<dg> dgs = new ArrayList<>();
public usr() {}
@Override public String toString()
{ return "(%d %s %s %s)"
.formatted(id, fn, ln, dgs
.stream()
.map(a -> "(%d %s %s)"
.formatted(a.id, a.nm, a.bd))
.toList()); }
}
@Table(name = tb2) @Entity @ToString
@RequiredArgsConstructor
public static class dg extends _$ {
@NonNull String nm;
@NonNull String bd;
@ManyToOne
@Nullable usr rs;
public dg() {}
}
}